假设我有User
和Group
个实体,并希望它们之间有1到多个关系。即每个User
属于一个Group
,而任何Group
都可以包含多个User
。
集团实体:
@Entity
@Table(name="GroupTable")
public class Group {
@Id
@Column(name="Id")
private long id;
@Column(name="Name")
private String name;
@OneToMany(mappedBy="group")
private Set<User> users;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
和用户实体:
@Entity
@Table(name="User")
public class User {
@Id
@Column(name="Id")
private long id;
@ManyToOne
@JoinColumn(name="GroupId")
private Group group;
@Column(name="Name")
private String name;
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
以下是应用程序代码:
public class App_Tester_Hibernate_01
{
private static Logger log = LoggerFactory.getLogger(App_Tester_Hibernate_01.class);
public static void main( String[] args )
{
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry;
SessionFactory sessionFactory;
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session;
session = sessionFactory.openSession();
session.beginTransaction();
Group group;
User user;
group = (Group) session.byId(Group.class).load(1l);
if( group == null ) {
group = new Group();
group.setId(1l);
group.setName("Group #1");
session.save(group);
}
log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));
user = (User) session.byId(User.class).load(1l);
if( user == null ) {
user = new User();
user.setId(1l);
user.setName("Bob");
user.setGroup(group);
session.save(user);
}
log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));
user = (User) session.byId(User.class).load(2l);
if( user == null ) {
user = new User();
user.setId(2l);
user.setName("Alice");
user.setGroup(group);
session.save(user);
}
log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));
user = (User) session.byId(User.class).load(3l);
if( user == null ) {
user = new User();
user.setId(3l);
user.setName("Peter");
user.setGroup(group);
session.save(user);
}
log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));
session.update(group);
log.info("Group.Users = {}", StringUtils.collectionToCommaDelimitedString(group.getUsers()));
session.getTransaction().commit();
session.close();
}
}
不幸的是,getUser()
总是返回null
。为什么?是不是设置为相关用户的代表?我做错了什么?
我应该自己进行收藏同步吗? Hibernate不会自动填充字段吗?
更新1
我的hibernate配置如下:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/tester_hibernate_01</property>
<property name="connection.username">root</property>
<property name="connection.password">sa</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<property name="hibernate.query.substitution">true 1, false 0, yes 'Y', no 'N'</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="test.Tester_Hibernate_01.Group"/>
<mapping class="test.Tester_Hibernate_01.User"/>
</session-factory>
</hibernate-configuration>
添加
@OneToMany(mappedBy="group", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<User> users;
没有帮助。
更新2
添加
Hibernate.initialize(group);
没有帮助,getUsers()
仍然返回null。
答案 0 :(得分:1)
有趣的例子:你正在使用2个事务,在你保存用户时分离了group-entity所以它不会被更新(实际上我是curius如果hibernate可以解决这个问题,如果附加了,我做不这么认为,如果你试试让我知道)。您可能想要做的是将用户添加到组实体中的集合,然后刷新它以便在用户实体中分配ID。但是,您需要添加的是对组实体(http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-cascade)中的集合字段进行适当的级联注释,以使其正常工作