hibernate基础12:关联映射之基于主键的双项多对多
Posted 来临
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate基础12:关联映射之基于主键的双项多对多相关的知识,希望对你有一定的参考价值。
1、Java实体bean类
package com.project.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Role implements Serializable{ private int id; private String name ; private Set<Resource> resources = 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<Resource> getResources() { return resources; } public void setResources(Set<Resource> resources) { this.resources = resources; } public void setResources(Resource resource) { this.getResources().add(resource); } }
package com.project.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Resource implements Serializable{ private int id; private String name; private Set<Role> roles = 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<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
2、hbm.xml配置
<?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 package="com.project.pojo"> <class name="Role" table="t_role"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <!-- table为连接表的表名 --> <set name="resources" table="t_role_resource"> <!-- key指明当前类在关联表中的外键 --> <key column="roid" /> <!-- column 多对多的另一方在关联表中的外键 --> <many-to-many column="reid" class="Resource"></many-to-many> </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 package="com.project.pojo"> <class name="Resource" table="t_resource"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <set name="roles" table="t_role_resource" inverse="true"> <key column="reid" /> <many-to-many column="roid" class="Role" /> </set> </class> </hibernate-mapping>
3、测试
package com.project.test; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.project.pojo.Resource; import com.project.pojo.Role; import com.project.util.HibernateUtil; public class HibernateTest { Session session = null; Transaction ts = null; @Before public void setUp(){ session = HibernateUtil.getSession(); ts = session.beginTransaction(); } @After public void tearDown(){ HibernateUtil.closeSession(); } @Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); //使得hibernate映射信息转换为数据库识别的dll语言 SchemaExport se = new SchemaExport(cfg); //第一个参数:是否打印dll语句 //第二个参数:是否将dll到数据库中执行 se.create(true, true); } @Test public void testInit(){ try { Role ro1 = new Role(); ro1.setName("普通用户"); Role ro2 = new Role(); ro2.setName("VIP"); Resource re1 = new Resource(); re1.setName("查看"); Resource re2 = new Resource(); re2.setName("新增"); Resource re3 = new Resource(); re3.setName("修改"); Resource re4 = new Resource(); re4.setName("删除"); session.save(re1); session.save(re2); session.save(re3); session.save(re4); //设置关系 ro1.setResources(re1); ro1.setResources(re2); ro2.setResources(re1); ro2.setResources(re3); ro2.setResources(re4); session.save(ro1); session.save(ro2); ts.commit(); } catch (Exception e) { e.printStackTrace(); if(ts!=null)ts.rollback(); } } @Test public void testSelect(){ Role ro = (Role) session.get(Role.class, 1); System.out.println(ro.getName()); System.out.println("-----------"); for(Resource r : ro.getResources()){ System.out.println(r.getName()); } System.out.println("============="); Resource re = (Resource) session.get(Resource.class, 1); System.out.println(re.getName()); System.out.println("-------------"); for(Role r : re.getRoles()){ System.out.println(r.getName()); } } }
以上是关于hibernate基础12:关联映射之基于主键的双项多对多的主要内容,如果未能解决你的问题,请参考以下文章