我知道這個問題之前已經被問過好幾次了。但我相信我有一個非常不同的情況,當我調用getResultList()方法時,當前遇到此錯誤,而不是持續時。getResultList()生成org.hibernate.TransientObjectException:對象引用一個未保存的瞬態實例
請注意,我使用: -javaee6 -seam3安全 -jboss7.1.3 -postgresql
事件的系列: 1)登錄後,我用救了我的用戶picketlink接口org.picketlink.idm.api.User:
setUser(new MyUser(user));
我應該能夠使用以檢索用戶((MYUSER)identity.getuser())的getUser();
2)現在我有了與AccounType一對多關係的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;
}
我的問題是我有一個服務:BusinessAccountService擴展BaseService,這就要求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:[[email protected], [email protected]]
,我發現,這行引發錯誤:
and (a.accountType in (:a_accountType0))
這是爲什麼?當我只執行一個getResultList()?