基于Annotation 的一对多ManyToOne单向和基于XML配一对多单向

基于Annotation:
多方:
package com.stark.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {
private int userId;
private String userName;
//private Group group;
@Id
@GeneratedValue
public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}

}
一方:
package com.stark.hibernate.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
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 groupId ;
private String groupName;
private Set<User> users= new HashSet<User>();
@Id
@GeneratedValue
public int getGroupId() {
return groupId;
}
public void setGroupId(int id) {
this.groupId = id;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String name) {
this.groupName = name;
}
/**
* One 代表Group,Many代表User ,我们需要在多的一方添加外键,如果不添加@JoinColumn(name="groupId"),将会生成一张t_group_t_user的中间表
* 否则将会在user表中自动添加groupId字段
* @return
*/
@OneToMany(cascade = { CascadeType.ALL})
@JoinColumn(name="groupId")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}





}


生成数据库DDL语句:
create table t_group (groupId integer not null auto_increment, groupName varchar(255), primary key (groupId))
create table t_user (userId integer not null auto_increment, userName varchar(255), groupId integer, primary key (userId))
alter table t_user add index FKCB63CCB65F00FCD6 (groupId), add constraint FKCB63CCB65F00FCD6 foreign key (groupId) references t_group (groupId)


@Test
public void saveGroup() {
Session session = sessionFactory.openSession();
session.beginTransaction();
Set<User>users= new HashSet<User>();
User u1= new User();
u1.setUserName("wu");
users.add(u1);
User u2= new User();
u2.setUserName("wy");
users.add(u2);
Group g =new Group();
g.setGroupName("student");
g.setUsers(users);
session.save(g);
session.getTransaction().commit();
session.close();
}
Hibernate: insert into t_group (groupName) values (?)
Hibernate: insert into t_user (userName) values (?)
Hibernate: insert into t_user (userName) values (?)
Hibernate: update t_user set groupId=? where userId=?
Hibernate: update t_user set groupId=? where userId=?
PS:我在OneToMany中添加了cascade = { CascadeType.ALL},所有不用先保存User了,但是感觉在实际项目开发中如User定已经先保存在数据库中了
@Test
public void getGroup(){
Session session = sessionFactory.openSession();
session.beginTransaction();
Group g=(Group) session.get(Group.class, 2);
System.out.println("groupId:"+g.getGroupId());
System.out.println("groupName:"+g.getGroupName());
Set<User>users=g.getUsers();
for(User u:users){
System.out.println("userId:"+u.getUserId());
System.out.println("userName:"+u.getUserName());
}
session.getTransaction().commit();
session.close();
}
执行结果为:
groupId:2
groupName:student
Hibernate: select users0_.groupId as groupId1_, users0_.userId as userId1_, users0_.userId as userId1_0_, users0_.userName as userName1_0_ from t_user users0_ where users0_.groupId=?
userId:2
userName:wy
userId:1
userName:wu


基于XML:
去掉User和Group中@注解
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 package="com.stark.hibernate.model">
<class name="Group" table="t_group">
<id name="groupId" >
<generator class="native"/>
</id>
<property name="groupName"/>
<set name="users">
<key column="groupId"></key>
<one-to-many class="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 package="com.stark.hibernate.model">
<class name="User" table="t_user">
<id name="userId">
<generator class="native"/>
</id>
<property name="userName" />
</class>
</hibernate-mapping>

生成数据库DDl语句:

create table t_group (groupId integer not null auto_increment, groupName varchar(255), primary key (groupId))
create table t_user (userId integer not null auto_increment, userName varchar(255), groupId integer, primary key (userId))

alter table t_user add index FKCB63CCB65F00FCD6 (groupId), add constraint FKCB63CCB65F00FCD6 foreign key (groupId) references t_group (groupId)
至于查询部分跟Annotation方法是相同的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值