映射一对多-02双向inverse
Posted shamgod
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了映射一对多-02双向inverse相关的知识,希望对你有一定的参考价值。
1.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 <class name="mypack.Monkey" table="MONKEYS"> 8 9 <id name="id" type="long" column="ID"> 10 <generator class="increment"/> 11 </id> 12 13 <property name="name" type="string" column="NAME" /> 14 15 <many-to-one 16 name="team" 17 column="TEAM_ID" 18 class="mypack.Team" 19 cascade="save-update" 20 /> 21 22 </class> 23 24 </hibernate-mapping>
2.
1 package mypack; 2 3 public class Monkey { 4 5 6 private long id; 7 private String name; 8 private Team team; 9 10 public Monkey() {} 11 12 public Monkey(String name, Team team) { 13 this.name = name; 14 this.team = team; 15 } 16 17 public long getId() { 18 return this.id; 19 } 20 21 public void setId(long id) { 22 this.id = id; 23 } 24 public String getName() { 25 return this.name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 public Team getTeam() { 32 return this.team; 33 } 34 35 public void setTeam(Team team) { 36 this.team = team; 37 } 38 }
3.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping > 6 7 <class name="mypack.Team" table="TEAMS" > 8 <id name="id" type="long" column="ID"> 9 <generator class="increment"/> 10 </id> 11 12 <property name="name" type="string" column="NAME" /> 13 <!-- 14 <set 15 name="monkeys" 16 cascade="all-delete-orphan" 17 inverse="true" 18 > 19 20 <key column="TEAM_ID" /> 21 <one-to-many class="mypack.Monkey" /> 22 </set> 23 --> 24 <set 25 name="monkeys" 26 inverse="true" 27 cascade="save-update" 28 > 29 30 <key column="TEAM_ID" /> 31 <one-to-many class="mypack.Monkey" /> 32 </set> 33 34 </class> 35 </hibernate-mapping>
4.
1 package mypack; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Team{ 7 8 private long id; 9 private String name; 10 private Set monkeys = new HashSet(); 11 12 public Team() {} 13 14 public Team(String name, Set monkeys) { 15 this.name = name; 16 this.monkeys = monkeys; 17 } 18 19 public long getId() { 20 return this.id; 21 } 22 23 public void setId(long id) { 24 this.id = id; 25 } 26 public String getName() { 27 return this.name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 public Set getMonkeys() { 34 return this.monkeys; 35 } 36 37 public void setMonkeys(Set monkeys) { 38 this.monkeys = monkeys; 39 } 40 }
5.
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-configuration 3 PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="dialect"> 8 org.hibernate.dialect.mysqlDialect 9 </property> 10 <property name="connection.driver_class"> 11 com.mysql.jdbc.Driver 12 </property> 13 <property name="connection.url"> 14 jdbc:mysql://localhost:3306/sampledb 15 </property> 16 <property name="connection.username"> 17 root 18 </property> 19 <property name="connection.password"> 20 1234 21 </property> 22 <property name="show_sql">true</property> 23 <mapping resource="mypack/Team.hbm.xml" /> 24 <mapping resource="mypack/Monkey.hbm.xml" /> 25 </session-factory> 26 </hibernate-configuration>
6.
以上是关于映射一对多-02双向inverse的主要内容,如果未能解决你的问题,请参考以下文章
Hibernate,关系映射的多对一单向关联多对一双向关联一对一主键关联一对一外键关联多对多关系关联