1数据库开发中经常会遇到一对多的关系,例如客户与订单的关系,一个客户对应多个订单。
开发一个customer与orders的demo
(1)领域模型
customer
package com.songxu.domain; import java.io.Serializable; import java.util.Set; public class Customer implements Serializable { private int id; private String name; private Set orders; 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 getOrders() { return orders; } public void setOrders(Set orders) { this.orders = orders; } }
package com.songxu.domain; import java.io.Serializable; import java.sql.Timestamp; public class Order implements Serializable { private int id; private Timestamp time; private Customer customer; public int getId() { return id; } public void setId(int id) { this.id = id; } public Timestamp getTime() { return time; } public void setTime(Timestamp time) { this.time = time; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
customer
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.songxu.domain.Customer" table="customer"> <id column="id" name="id" type="int"> <generator class="increment"/> </id> <!-- column同名时可以省略 --> <property column="name" generated="never" lazy="false" name="name" type="string"/> <set name="orders" cascade="save-update" inverse="true" > <key column="customer" ></key> <one-to-many class="com.songxu.domain.Order"/> </set> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.songxu.domain.Order" table="orders"> <id column="id" name="id" type="int"> <generator class="increment"/> </id> <!-- column同名时可以省略 --> <property column="occurTime" generated="never" lazy="false" name="time" type="timestamp"/> <many-to-one name="customer" class="com.songxu.domain.Customer" column="customer_id"></many-to-one> </class> </hibernate-mapping>
(1) 领域模型
package com.songxu.domain; import java.util.Set; public class Food { private int id; private String name ; private Food parentFood; private Set<Food> childFood; public int getId() { return id; } public Food() { } public Food(String name, Food parentFood, Set<Food> childFood) { this.name = name; this.parentFood = parentFood; this.childFood = childFood; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Food getParentFood() { return parentFood; } public void setParentFood(Food parentFood) { this.parentFood = parentFood; } public Set<Food> getChildFood() { return childFood; } public void setChildFood(Set<Food> childFood) { this.childFood = childFood; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.songxu.domain.Food" table="food"> <id column="id" name="id" type="int"> <generator class="increment" /> </id> <!-- column同名时可以省略 --> <property column="name" generated="never" lazy="false" name="name" type="string" /> <many-to-one name="parentFood" class="com.songxu.domain.Food" column="foodId"></many-to-one> <set name="childFood" cascade="all"> <key column="foodId"></key> <one-to-many class="com.songxu.domain.Food" /> </set> </class> </hibernate-mapping>
package com.songxu.test; import java.util.HashSet; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.songxu.domain.Food; public class FoodTest { static Configuration configuration; static SessionFactory sessionFactory; static { configuration=new Configuration().configure(); sessionFactory=configuration.buildSessionFactory(); } public static void main(String[] args) { Session session=sessionFactory.openSession(); Transaction tx=null; try { tx=session.beginTransaction(); Food food=new Food("食品",null,new HashSet<Food>()); Food fruit=new Food("水果",null,new HashSet<Food>()); Food vegetables=new Food("蔬菜",null,new HashSet<Food>()); food.getChildFood().add(fruit); food.getChildFood().add(vegetables); fruit.setParentFood(food); vegetables.setParentFood(food); Food fruit1=new Food("苹果",null,new HashSet<Food>()); Food fruit2=new Food("香蕉",null,new HashSet<Food>()); Food v1=new Food("黄瓜",null,new HashSet<Food>()); Food v2=new Food("茄子",null,new HashSet<Food>()); fruit.getChildFood().add(fruit1); fruit.getChildFood().add(fruit2); fruit1.setParentFood(fruit); fruit2.setParentFood(fruit); vegetables.getChildFood().add(v1); vegetables.getChildFood().add(v2); v1.setParentFood(vegetables); v2.setParentFood(vegetables); //仅保存根对象就可以了 session.save(food); //session.get(Food.class,1); tx.commit(); } catch (Exception e) { if(null!=tx) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。