hibernate之关于使用连接表实现多对一关联映射
@Entity @Table(name="t_group") publicclass Group { private Integer id; private String name; @Id @GeneratedValue public Integer getId() { returnid; } publicvoid setId(Integer id) { this.id = id; } @Column(name="g_name") public String getName() { returnname; } publicvoid setName(String name) { this.name = name; } }
@Entity @Table(name="p_person") publicclass Person { private Integer id; private String name; private Integer age; private Group group; @ManyToOne
//下面是配置中间表的核心 @JoinTable(name="g_p",joinColumns={@JoinColumn(name="p_id")}, inverseJoinColumns={@JoinColumn(name="g_id")}) public Group getGroup() { returngroup; } publicvoid setGroup(Group group) { this.group = group; } @Id @GeneratedValue public Integer getId() { returnid; } publicvoid setId(Integer id) { this.id = id; } @Column(name="p_name") public String getName() { returnname; } publicvoid setName(String name) { this.name = name; } @Column(name="p_age") public Integer getAge() { returnage; } publicvoid setAge(Integer age) { this.age = age; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="csg.hibernate.entity"> <class name="Person" table="t_person"> <id name="id"> <column name="id"/> <generator class="native" /> </id> <property name="name" /> <property name="age" /> <join table="t_p" optional="true"> <key column="p_id"></key> <many-to-one name="group" column="g_id" class="Group" unique="true"/> </join> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="csg.hibernate.entity"> <class name="Group" table="t_group"> <id name="id"> <column name="id"/> <generator class="native" /> </id> <property name="name" /> </class> </hibernate-mapping>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。