数据库略 Customer.java package pack; import java.util.HashSet; import java.util.Set; public class Customer { int id ; String name; String password; private Set products=new HashSet(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Set getProducts() { return products; } public void setProducts(Set products) { this.products = products; } } Product.java package pack; import java.util.HashSet; import java.util.Set; public class Product { int id; String name; private Set customers=new HashSet(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getCustomers() { return customers; } public void setCustomers(Set customers) { this.customers = customers; } } Customer.hbm.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="pack"> <class name="Customer" table="customer"> <id name="id" type="java.lang.Integer" column="id"> <generator class="native"></generator> </id> <property name="name" column="name" length="20"></property> <property name="password" column="password" length="20"></property> <set name="products" table="customer_product" lazy="true" cascade="all"> <key column="customerid"></key> <many-to-many class="Product" column="productid"></many-to-many> </set> </class> </hibernate-mapping> Product.hbm.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="pack"> <class name="Product" table="product"> <id name="id" type="java.lang.Integer" column="id"> <generator class="native"></generator> </id> <property name="name" column="name" length="20"></property> <set name="customers" table="customer_product" lazy="true" inverse="true" cascade="all"> <key column="productid"></key> <many-to-many class="Customer" column="customerid"> </many-to-many> </set> </class> </hibernate-mapping> 测试文件Test.java package pack; import java.util.Iterator; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Test { /** * @param args */ Session session; SessionFactory sessionFactory; Test()//构造函数 在创建对象的时候执行 { sessionFactory = new Configuration().configure().buildSessionFactory(); this.session =sessionFactory.openSession(); } public void get() { Transaction tx = session.beginTransaction(); Product product1=new Product(); product1.setName("computer"); Product product2=new Product(); product2.setName("printer"); Customer customer1=new Customer(); customer1.setName("lf45"); customer1.setPassword("2123"); customer1.getProducts().add(product1); customer1.getProducts().add(product2); product1.getCustomers().add(customer1); product2.getCustomers().add(customer1); Customer customer2=new Customer(); customer2.setName("lflf123"); customer2.setPassword("3214"); customer2.getProducts().add(product1); product1.getCustomers().add(customer2); session.save(customer1); session.save(customer2); Iterator iter=customer1.getProducts().iterator(); while(iter.hasNext()) { Product products=(Product) iter.next(); System.out.println(products.getName()); } Iterator iter1=product1.getCustomers().iterator(); while(iter1.hasNext()) { Customer customers=(Customer) iter1.next(); System.out.println(customers.getName()); } session.flush(); tx.commit(); session.close(); } public void delete() { Transaction tx = session.beginTransaction(); Customer customer=(Customer) session.load(Customer.class, new Integer(7)); session.delete(customer); tx.commit(); session.close(); } public static void main(String[] args) { Test tt = new Test(); tt.get(); // tt.delete(); } } 本文代码已经在myeclipse和mysql上测试通过。