标签:pac sql语句 .com cte blog 映射文件 eve 文件中 one
一对多关联映射: 在多的一端添加一个外键指向一的一端,它维护的关系是一指向多 多对一关联映射: 咋多的一端加入一个外键指向一的一端,它维护的关系是多指向一
在配置文件中添加: 在一一端使用<set><key></key><one-to-many></one-to-many></set> <key>指定的外键字段必须和<many-to-one>指定的外键字段一致 在多一端使用<many-to-one>
public class Customer { private Integer id; private String name; private Character gender; private Integer age; private String level; //一对多 private Set<Order> orders = new HashSet<Order>(); public Customer() { super(); }
getters and setters }
public class Order { private Integer id; private String orderno; private String productName; //多对一 private Customer customer; public Order() { super(); // TODO Auto-generated constructor stub }
getters and setters }
<hibernate-mapping> <class name="com.roxy.hibernate.pojo.Customer" table="t_customer"> <id name="id" column="c_id"> <generator class="native"></generator> </id> <property name="name" column="c_name" not-null="true"></property> <property name="gender" column="c_gender" length="1"></property> <property name="age" column="c_age"></property> <property name="level" column="c_level" length="20"></property> <set name="orders"> <key column="customer_id"></key> <one-to-many class="com.roxy.hibernate.pojo.Order"/> </set> </class> </hibernate-mapping>
<hibernate-mapping package="com.roxy.hibernate.pojo"> <!-- 类和表的映射 --> <class name="Order" table="t_order"> <id name="id" column="id"> <generator class="native"></generator> </id> <!-- 其他属性映射 --> <property name="orderno" column="orderno" length="20"></property> <property name="productName" column="product_name" length="100"></property> <!-- 多对一 --> <many-to-one name="customer" class="Customer" column="customer_id" /> </class> </hibernate-mapping>
<!-- mapping文件 --> <mapping resource="com/roxy/hibernate/pojo/Customer.hbm.xml"/> <mapping resource="com/roxy/hibernate/pojo/Order.hbm.xml"/>
Hibernate: alter table t_order add constraint FKesy3n2gc3fa0s3trrk3tvyv9a foreign key (customer_id) references t_customer (c_id)
----- hibernate首先为t_order和t_customer添加外键关联
Hibernate: insert into t_customer (c_name, c_gender, c_age, c_level) values (?, ?, ?, ?)
----- 向t_customer中插入数据(session.save(cust);)
Hibernate: insert into t_order (orderno, product_name, customer_id) values (?, ?, ?)
----- 向t_order中插入数据(session.save(o1);)
Hibernate: insert into t_order (orderno, product_name, customer_id) values (?, ?, ?)
----- 向t_order中插入数据(session.save(o2);)
Hibernate: update t_order set customer_id=? where id=?
----- 维护两个表之间的关系,hibernate更新t_customer数据
Hibernate: update t_order set customer_id=? where id=?
----- 维护两个表之间的关系,hibernate更新t_customer数据
标签:pac sql语句 .com cte blog 映射文件 eve 文件中 one
原文地址:http://www.cnblogs.com/roxy/p/7608778.html