043:hibernate:一对多单向关联(Annotion)
来源:互联网 发布:机械图纸设计软件 编辑:程序博客网 时间:2024/06/16 18:52
1、在添加 @OneToMany标签时要加上@JoinColumn(name="groupId")
只写@OneToMany它会认为存在一张中间表
同时添加 private Set<User> users = new HashSet<User>();
package com.bjsxt.hibernate;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="t_group")public class Group {private int id;private String name;private Set<User> users = new HashSet<User>();@Id@GeneratedValuepublic 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;}@OneToMany@JoinColumn(name="groupId")public Set<User> getUsers() {return users;}public void setUsers(Set<User> users) {this.users = users;}}
package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="t_user")public class User {private int id;private String name;@Id@GeneratedValuepublic 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;}}
2、
<mapping class="com.bjsxt.hibernate.Group"/><mapping class="com.bjsxt.hibernate.User"/>
*************************************************************************************************
在xml中:
对Group.hbm.xml:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.bjsxt.hibernate.Group" table="t_group"><id name="id"><generator class="native"></generator></id><property name="name"></property><set name="users"><key column="groupId"></key><one-to-many class="com.bjsxt.hibernate.User"/></set> </class></hibernate-mapping>
对user.hbm.xml:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.bjsxt.hibernate.User" table="t_user"><id name="id"><generator class="native"></generator></id><property name="name"></property> </class></hibernate-mapping>
<mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/> <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
0 0
- 043:hibernate:一对多单向关联(Annotion)
- Hibernate一对多单向关联
- hibernate一对多单向关联
- hibernate一对多单向关联
- Hibernate单向一对多关联
- hibernate一对多单向关联
- Hibernate 一对多单向关联
- hibernate 一对多关联 (1)单向关联
- hibernate 关联关系 一对多 单向关联 .
- hibernate中----一对多单向关联,一对多双向关联
- Hibernate关联关系映射(单向一对多、单向多对一、双向一对多)
- 【Hibernate步步为营】--(一对多映射)之单向关联
- 【Hibernate步步为营】--(一对多映射)之单向关联
- 【Hibernate步步为营】--(一对多映射)之单向关联
- Hibernate(五)——一对多单向关联映射
- 【Hibernate步步为营】--(一对多映射)之单向关联
- Hibernate的一对多关联关系(单向和双向)
- hibernate-关系映射(一对多单向关联)-9
- (3)windows下hadoop+eclipse环境搭建
- effective c++ tip3:尽可能使用const
- Codeforces Round #260 (Div. 1) A. Boredom
- 稀疏自编码器及其实现——如何搞基
- HttpClient 学习整理
- 043:hibernate:一对多单向关联(Annotion)
- UVa 103 - Stacking Boxes (LIS,打印路径)
- lightoj 1024 (高精度乘单精度)
- 05-0. 求序列前N项和(15)
- Java中Synth外观学习(八)--List的定制二,带滚动条的List
- Shell 文件描述符
- POJ3716-Panda's Birthday Present题解
- Java这个名字怎么来
- hdu1028 Ignatius and the Princess III