【发布时间】:2016-07-11 02:06:58
【问题描述】:
我的问题是,当在以下对象上获取实例时,hibernate 在 @OneToMany Set organizationMemberCollection 的值中检索 null:
UserAccount.java:
@Entity
@Table(name="USER_ACCOUNT")
public class UserAccount {
@Id
@Column(name = "id", nullable = false)
@SequenceGenerator(name = "generator", sequenceName = "USER_ACCOUNT_id_seq", allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
private Long id;
@Column(name = "EMAIL", nullable = false)
private String email;
@Column(name = "PASSWORD_HASH")
private String passwordHash;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userAccount")
private Set <OrganizationMember> organizationMemberCollection;
...
/*
* getters and setters
*/
}
这是“拥有”关联的对象:
OrganizationMember.java:
@Entity
@Table(name="ORGANIZATION_MEMBER")
public class OrganizationMember{
@Id
@Column(name = "id", nullable = false)
@SequenceGenerator(name = "generator", sequenceName = "ORGANIZATION_MEMBER_id_seq", allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ACCOUNT_ID", nullable = false)
private UserAccount userAccount;
...
/*
* getters and setters
*/
}
在这个应用程序中,我们有两种不同的配置:
-
生产,其中 Hibernate 连接到 PostgreSQL 数据库。 这是 prod 的 sessionFactory 配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="hibernateProperties"> <props> <prop key="hibernate.jdbc.batch_size">10</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cglib.use_reflection_optimizer">false</prop> </props> </property> ... </bean> -
测试,其中 Hibernate 连接到内存中的 HSQLDB 数据库:
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.cglib.use_reflection_optimizer">false</prop> <prop key="hibernate.hbm2ddl.auto">create-drop</prop> <prop key="hibernate.cache.use_second_level_cache">false</prop> <prop key="hibernate.cache.use_query_cache">false</prop> </props> </property> ... </bean>
此问题仅在测试配置中出现;在生产配置中,一切都很顺利,我可以得到收藏。
但是,当我在测试配置中获取 UserAccount 时,我会在 organizationMemberCollection 属性中得到 null(不是空集)。
通过 google 和 Hibernate 的文档进行了几个小时的研究后,我仍然没有找到任何与相同问题/行为相关的帖子,所以我有点迷茫,非常感谢您的帮助!
如果需要,我当然可以提供更多信息,谢谢!
编辑:
测试突出问题:
@Test
@Transactional
public void testFindUserAccount_OrganizationMemberCollectionFetching() {
assertNotNull(userAccountDao.findUserAccount("user1@test.fr")); //NoProblem
assertNotNull(userAccountDao.findUserAccount("user1@test.fr").getCabinetMemberCollection()); //Fails
}
跟下面findUserAccountdao
public UserAccount findUserAccount(String email) {
if (email == null) {
return null;
}
UserAccount userAccount = (UserAccount) this.sessionFactory
.getCurrentSession().createCriteria(UserAccount.class)
.add(Restrictions.eq("email", email).ignoreCase())
.uniqueResult();
if (userAccount == null) {
throw new ObjectNotFoundException("UserAccount.notFound");
} else {
return userAccount;
}
}
【问题讨论】:
-
您得到一个空值,因为您没有任何与您的 userAccount 关联的组织成员集合。你能提供你的测试代码吗?也许你在保存 OrganizationMember 之前忘记调用 setUserAccount 方法
-
感谢您的快速回答,我认为在这种情况下,Hibernate 会返回我的 UserAccount 为空 Set,而不是 null 值,但也许我错了。我很确定 OrganizationMember 引用了用户帐户,因为数据库在测试之前就已填充。当我获取 OrganizationMember 时,hibernate 返回的对象具有对 UserAccount 的引用。
-
您确定在测试中调用 getOrganizationMemberCollection 时持久性上下文没有关闭吗?如果我们能看到代码也许会有所帮助
-
是的,我正在使用 Spring 来处理事务。我编辑帖子以包含显示问题的测试,谢谢!
标签: java hibernate one-to-many hibernate-onetomany