我知道之前已经多次询问过这个问题。但我相信我有一个非常不同的情况,我在调用getResultList()方法时遇到此错误,而不是在持久化时。
请注意我正在使用: -javaee6 -seam3安全 -jboss7.1.3 -postgresql
系列活动: 1.)登录后,我使用org.picketlink.idm.api.User接口将我的用户保存在picketlink中:
setUser(new MyUser(user));
我应该能够使用((MyUser)identity.getuser())来检索用户.getUser();
2。)现在我有一个与AccounType具有OneToMany关系的BusinessAccount实体:
@Entity
@Table(name = "T_ACCOUNT")
public class BusinessAccount extends BaseEntity {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ACCOUNT_TYPE_ID")
private AccountType accountType;
}
@Entity()
@Table(name = "T_ACCOUNT_TYPE")
public class AccountType extends BaseEntity {
@Enumerated(EnumType.STRING)
@Column(name = "ACCOUNT_TYPE", length = 20)
private AccountTypeEnum accountTypeEnum;
}
我的问题是我有一个服务:扩展BaseService的BusinessAccountService,它调用getAccounts并抛出此错误:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing
BaseService有:
public User getCurrentUser() {
if(currentUser==null){
try {
currentUser=((MyUser) identity.getUser()).getUser();
} catch(Exception e){
log.warn("getCurrentUser cannot retrieve current user from session identity and currentUser has not been set programmatically");
}
}
return currentUser;
}
然后我动态生成的查询是这样的:
select distinct a from BusinessAccount a where a.accountType.accountTypeEnum=:a_accountType_accountTypeEnum and (a.accountType in (:a_accountType0))
Param name:a_accountType_accountTypeEnum value:serviceProvider
Param name:a_accountType0 value:[org.model.accounts.AccountType@1, org.model.accounts.AccountType@2]
我发现这一行引发了错误:
and (a.accountType in (:a_accountType0))
为什么?当我只执行getResultList()?
时答案 0 :(得分:0)
我认为我发现了问题,AccountType正在扩展具有Version字段的BaseEntity。当我删除扩展并在AccountType中添加Version属性时,我得到了同样的错误。
版本字段:
@Version
@Column(name = "VERSION")
private Integer version;
此版本字段的初始值必须为0.