1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JPA关系映射之one-to-many和many-to-one

JPA关系映射之one-to-many和many-to-one

时间:2019-10-11 03:23:02

相关推荐

JPA关系映射之one-to-many和many-to-one

one-to-many(一对多)和many-to-one(多对一)双向关联

假设部门与员工是一对多关系,反过来员工与部门就是多对一关系。

Dept.java类

1 public class Dept implements java.io.Serializable { 2 3// Fields 4 5private Integer deptId; 6private String deptName; 7private Set<Emp> emps=new HashSet<Emp>(); 8 9// Constructors10 11/** default constructor */12public Dept() {13}14 15/** full constructor */16public Dept(String deptName) {17 this.deptName = deptName;18}19 20// Property accessors21 22public Integer getDeptId() {23 return this.deptId;24}25 26public void setDeptId(Integer deptId) {27 this.deptId = deptId;28}29 30public String getDeptName() {31 return this.deptName;32}33 34public void setDeptName(String deptName) {35 this.deptName = deptName;36}37 38public Set<Emp> getEmps() {39 return emps;40}41 42public void setEmps(Set<Emp> emps) {43 this.emps = emps;44}45 46 }

Emp.java类

1 public class Emp implements java.io.Serializable { 2 3// Fields 4 5private Integer empNo; 6private String empName; 7private Date empBrithday; 8private Dept dept; 9 10// Constructors11 12/** default constructor */13public Emp() {14}15 16/** full constructor */17public Emp(String empName, Date empBrithday) {18 this.empName = empName;19 this.empBrithday = empBrithday;20}21 22// Property accessors23 24public Integer getEmpNo() {25 return this.empNo;26}27 28public void setEmpNo(Integer empNo) {29 this.empNo = empNo;30}31 32public String getEmpName() {33 return this.empName;34}35 36public void setEmpName(String empName) {37 this.empName = empName;38}39 40public Date getEmpBrithday() {41 return this.empBrithday;42}43 44public void setEmpBrithday(Date empBrithday) {45 this.empBrithday = empBrithday;46}47 48public Dept getDept() {49 return dept;50}51 52public void setDept(Dept dept) {53 this.dept = dept;54}55 56 }

Dept.hbm.xml

1 <hibernate-mapping> 2<class name="com.db.entity.Dept" table="dept" catalog="mydb"> 3 <id name="deptId" type="java.lang.Integer"> 4 <column name="deptId" /> 5 <generator class="native" /> 6 </id> 7 <property name="deptName" type="java.lang.String"> 8 <column name="deptName" length="32" /> 9 </property>10 <set name="emps" inverse="true" cascade="all">11 <key column="deptId" not-null="true" />12 <one-to-many class="com.db.entity.Emp" />13 </set>14</class>15 </hibernate-mapping>

Emp.hbm.xml

1 <hibernate-mapping> 2<class name="com.db.entity.Emp" table="emp" catalog="mydb"> 3 <id name="empNo" type="java.lang.Integer"> 4 <column name="empNo" /> 5 <generator class="native" /> 6 </id> 7 <property name="empName" type="java.lang.String"> 8 <column name="empName" length="20" /> 9 </property>10 <property name="empBrithday" type="java.util.Date">11 <column name="empBrithday"/>12 </property>13 <many-to-one name="dept" column="deptId" class="com.db.entity.Dept" not-null="true" fetch="select" cascade="save-update,delete"/>14</class>15 </hibernate-mapping>

hibernate.cfg.xml

1 <hibernate-configuration> 2 3<session-factory> 4 <property name="dialect"> 5 org.hibernate.dialect.MySQLDialect 6 </property> 7 <property name="connection.url"> 8 jdbc:mysql://localhost:3306/mydb 9 </property>10 <property name="connection.username">root</property>11 <property name="connection.password">123456</property>12 <property name="connection.driver_class">13 com.mysql.jdbc.Driver14 </property>15 <property name="myeclipse.connection.profile">16 MyDBAccount17 </property>18 <mapping resource="com/db/entity/Dept.hbm.xml" />19 <mapping resource="com/db/entity/Emp.hbm.xml" />20</session-factory>21 22 </hibernate-configuration>

测试用例

1 public class TestOneMany { 2 3public static void main(String[] args) throws ParseException { 4 // TODO Auto-generated method stub 5 6 Session session=HibernateSessionFactory.getSession(); 7 Dept dept1=new Dept(); 8 dept1.setDeptName("开发部"); 9 Emp emp1=new Emp();10 emp1.setEmpName("王洋");11 String brithString="1999-03-05";12 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");13 Date brithday=sdf.parse(brithString);14 emp1.setEmpBrithday(brithday);15 Emp emp2=new Emp();16 emp2.setEmpName("李林");17 brithString="-02-07";18 brithday=sdf.parse(brithString);19 emp2.setEmpBrithday(brithday);20 //把emp和dpt分别添加到对方的实力对象中21 dept1.getEmps().add(emp1);22 dept1.getEmps().add(emp2);23 emp1.setDept(dept1);24 emp2.setDept(dept1);25 session.beginTransaction();26 session.save(dept1);27 session.getTransaction().commit();28}29 30 }

注意:

在一的set配置中声明inverse=“true”,意味着Dept不在作为主控方,将关联关系的维护工作交给关联对象Emp来完成。在保存dept1对象的时不在关心Emp类中dept属性对应的deptId列,必须由Emp自己去维护,即设置emp.setDept(dept)

上述操作完成的sql语句是:

Hibernate: insert into mydb.dept (deptName) values (?)

Hibernate: insert into mydb.emp (empName, empBrithday, deptId) values (?, ?, ?)

Hibernate: insert into mydb.emp (empName, empBrithday, deptId) values (?, ?, ?)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。