hibernate(单向连接配置)
简捷开发
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
单向关联
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
实例1、
----------------------------------------------------------
many-to-one
类文件 添加属性
private customer customerId;
映射文件
<!-- <many-to-one name="customerId" not-null="true"/> -->
测试类:
Customer c=new Customer("tudou","123");
User u=new User("digua","123");
u.setCustomerId(c);
session.save(c);
session.save(u);
----------------------------------------------------------
one-to-one
类文件同上
<!-- <many-to-one name="customerId" not-null="true" unique="true"/> -->
测试类:同上
----------------------------------------------------------
one-to-many
类文件 添加属性
private Set customerId=new HashSet();
映射文件
<set name="customerId" fetch="join" lazy="false" cascade="save-update,persist" >
<key column="userId"
not-null="true"/>
<one-to-many
class="com.feng.pojo.Customer" />
</set>
测试类:
Customer c=new Customer("tudou","123");
Customer c2=new Customer("tudou2","123");
User u=new User("digua","123");
Set customers=new HashSet();
customers.add(c);
customers.add(c2);
u.setCustomerId(customers);
...
session.save(u);
session.save(c);
----------------------------------------------------------
????????????????????????????
<!--使用连接表的单向关联-->
实例2、
<!-- one-to-many -->
类文件 添加属性:同上
映射文件:
<set name="customerId" fetch="join" lazy="false" cascade="save-update,persist" >
<key column="userId"
not-null="true"/>
<many-to-many
class="com.feng.pojo.Customer" unique="true"/>
</set>
测试类:同上
----------------------------------------------------------
<!-- many-to-one-->
类文件 添加属性:同上
映射文件:
<join table="jointable" optional="true">
<key column="userId" unique="true"/>
<many-to-one name="customerId" not-null="true"/>
</join>
测试类:同上
----------------------------------------------------------
<!-- one-to-one-->
类文件 添加属性:同上
映射文件:
<join table="jointable" optional="true">
<key column="userId" unique="true"/>
<many-to-one name="customerId" not-null="true" unique="true"/>
</join>
测试类:同上
----------------------------------------------------------
<!-- many-to-many(单向)-->
类文件 添加属性:
相关类添加属性 例如:private Set CustomerId;
相关类添加
映射文件:
<set name="customerId" cascade="save-update" lazy="false" table="jointable">
<key column="userId"/>
<many-to-many column="customerId"
class="com.feng.pojo.Customer"/>
</set>
测试类:
Customer c=new Customer("tudou","123");
Customer c2=new Customer("tudou","123");
User u=new User("digua","123");
User u2=new User("digua","123");
Set customers=new HashSet();
Set users=new HashSet();
users.add(u);
users.add(u2);
customers.add(c);
customers.add(c2);
u.setCustomerId(customers);
/*c.setUserId(users);*/
Configuration configuration=new Configuration();
configuration.configure("/hibernate.cfg.xml");
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction trans=session.beginTransaction();
session.save(c);
session.save(u);
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
双向关联
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++