假设我有User
和Group
实体并希望它们之间有一对多的关系。即每个都User
属于一个Group
,而anyGroup
可以包含许多User
s。
集团实体:
@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-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。