hibernate: many-to-many

publicclassUser{

privateintid;

privateStringfirstName;

privateStringlastName;

privateDatebirthday;

privateSet<Skill>skills=newHashSet<Skill>();

**********************************************************************

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingpackage="com.entity.User">

<classname="User"table="user">

<idname="id">

<generatorclass="native"></generator>

</id>

<propertyname="first_name"length="50"></property>

<propertyname="last_name"length="50"></property>

<propertyname="birthday"length="80"></property>

<!--配置User和skill多对多的关联关系-->

<!--

lazy:延迟加载

inverse="false":用于表示双向关联中的被动一端。inverse的值为false的一方负责维护关联关系

save-update:更新当前对象时,级联保存,更新附属对象

fetch="select":默认的表示查询抓取

-->

<setaccess="property"lazy="true"inverse="false"

cascade="save-update"name="skills"batch-size="10"fetch="select"

table="user_skill">

<keycolumn="fk_user_id"/>

<many-to-manyclass="com.entity.Skill"

column="fk_skill_id"/>

</set>

</class>

</hibernate-mapping>

************************************************************************

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mappingpackage="com.entity.Skill">

<classname="Skill"table="skill">

<idname="id">

<generatorclass="native"></generator>

</id>

<propertyname="name"length="50"></property>

<propertyname="description"length="50"></property>

<!--配置User和skill多对多的关联关系-->

<!--

lazy:延迟加载

inverse="false":用于表示双向关联中的被动一端。inverse的值为false的一方负责维护关联关系

save-update:更新当前对象时,级联保存,更新附属对象

fetch="select":默认的表示查询抓取

-->

<setaccess="property"lazy="true"inverse="false"

cascade="save-update"name="users"batch-size="10"fetch="select"

table="user_skill">

<keycolumn="fk_skill_id"/>

<many-to-manyclass="com.entity.User"

column="fk_user_id"/>

</set>

</class>

</hibernate-mapping>

****************************************************************

publicvoidsave(){

Sessionsession=HibernateSessionFactory.getSession();

session.beginTransaction();

//createSkill

Skills1=newSkill();

Skills2=newSkill();

s1.setNeme("技能");

s2.setNeme("Java");

//createteacher

Useru1=newUser();

Useru2=newUser();

u1.setFirstName("zhang");

u2.setLastName("san");

//createrelationship

s1.getUsers().add(u1);

s1.getUsers().add(u2);

u1.getSkills().add(s1);

u2.getSkills().add(s1);

/*因为主控方级联设置为save-update,如果设置为none,则下面被注释的代码需要开启,否则会报错*/

//session.save(t1);

//session.save(t2);

session.save(s1);

session.getTransaction().commit();

session.close();

}

*************************************************************

/**

*多对多主控方删除(可以删除中间表记录)

*/

publicvoidtestDelete(){

Stringid="402881ee175f04be01175f04c05d0001";

Sessionsession=HibernateSessionFactory.getSession();

session.beginTransaction();

Skillc1=(Skill)session.get(Skill.class,id);

session.delete(c1);

session.getTransaction().commit();

session.close();

}

/**

*多对多被控方删除(无法删除中间表记录)

*/

publicvoidtestDeleteByInverse(){

Stringid="402881ee175a2e7c01175a2e7ead0003";

Sessionsession=HibernateSessionFactory.getSession();

session.beginTransaction();

Usert1=(User)session.get(User.class,id);

session.delete(t1);

session.getTransaction().commit();

session.close();

}

相关推荐