对象是用户User和用户组Group
1.这是表,通过外键关联
CREATE TABLE `group` (
`id` int(11) NOT NULL auto_increment COMMENT '编号',
`name` varchar(255) default NULL COMMENT '名称',
PRIMARY KEY (`id`)
);
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment COMMENT '编号',
`name` varchar(255) default NULL COMMENT '姓名',
`groupid` int(11) default NULL COMMENT '卡号',
PRIMARY KEY (`id`),
KEY `fk_1` (`groupid`),
CONSTRAINT `fk_1` FOREIGN KEY (`groupid`) REFERENCES `group` (`id`)
);
2.这是映射的实体类Group
package entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "group", catalog = "test")
public class Group implements java.io.Serializable
{
private Integer id;
private String name;
private Set<User> users = new HashSet<User>(0);
public Group()
{
}
public Group(String name, Set<User> users)
{
this.name = name;
this.users = users;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId()
{
return this.id;
}
public void setId(Integer id)
{
this.id = id;
}
@Column(name = "name")
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
//mappedBy指定了由多的那一方来维护关联关系
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "group")
public Set<User> getUsers()
{
return this.users;
}
public void setUsers(Set<User> users)
{
this.users = users;
}
}
3.这是实体类User
package entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "user", catalog = "test")
public class User implements java.io.Serializable
{
private Integer id;
private Group group;
private String name;
public User()
{
}
public User(Group group, String name)
{
this.group = group;
this.name = name;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId()
{
return this.id;
}
public void setId(Integer id)
{
this.id = id;
}
//通过 JoinColumn 的name属性指定了外键的名称
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "groupid")
public Group getGroup()
{
return this.group;
}
public void setGroup(Group group)
{
this.group = group;
}
@Column(name = "name")
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
}
4.这是测试类
public static void main(String[] args)
{
Set<User> users=new HashSet<User>();
User user=new User();
user.setName("张三");
users.add(user);
user=new User();
user.setName("李四");
users.add(user);
Group group=new Group();
group.setName("系统管理员");
group.setUsers(users);
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
//3个对象都保存了,但是user表groupidziduan字段没有值
session.save(group);
session.save(user);
session.getTransaction().commit();
session.close();
sf.close();
}
不知道怎么设置,谢谢大家。
3 个解决方案
#1
你这个是单向关联吧。你把你的user的group都set成你New出来的group试试
#2
是双
向关联
#3
我试下我试下
#1
你这个是单向关联吧。你把你的user的group都set成你New出来的group试试
#2
是双
向关联
#3
我试下我试下