对象引用了一个未保存的瞬态实例 - Spring,JPA Hibernate
Posted
技术标签:
【中文标题】对象引用了一个未保存的瞬态实例 - Spring,JPA Hibernate【英文标题】:object references an unsaved transient instance - Spring, JPA Hibernate 【发布时间】:2013-08-19 03:15:36 【问题描述】:代码如下:
@Entity
public class PortalUser
@NotNull
@OneToMany(mappedBy = "portalUser", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<PortalUserOrganisation> portalUserOrganisations;
@NotNull
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "portalUser", orphanRemoval = true)
private Set<UserRole> userRoles = new HashSet<UserRole>();
@Entity
public class PortalUserOrganisation
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID", referencedColumnName = "ID")
private PortalUser portalUser;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ORGANISATION_ID", referencedColumnName = "ID")
private Organisation organisation;
@Entity
public class Organisation
@OneToMany(mappedBy = "organisation", cascade = CascadeType.PERSIST, CascadeType.MERGE )
private Set<PortalUserOrganisation> portalUserOrganisations;
@Entity
public class UserRole
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID", referencedColumnName = "ID")
private PortalUser portalUser;
@NotNull
@ManyToOne(fetch = FetchType.LAZY, optional=true)
@JoinColumn(name = "ROLE_ID", referencedColumnName = "ID")
private RoleLookup roleLookup;
@Entity
public class RoleLookup extends AbstractLookupEntity
@OneToMany(mappedBy = "roleLookup", cascade = CascadeType.PERSIST, CascadeType.MERGE )
private Set<UserRole> userRoles = new HashSet<UserRole>();
Code to Create a User:
@Transactional
saveUser(userObj)
PortalUser portalUser = new PortalUser;
portalUser.setStatus(status);
PortalUserOrganisation userOrganisation = null;
for (OrganisationsDto dto : organisationsList())
userOrganisation = new PortalUserOrganisation();
userOrganisation.setOrganisation(organisationRepository.findOne(dto.getId()));
userOrganisation.setPortalUser(portalUser);
userOrganisation.setCreatedUpdatedBy(context.getName());
userOrganisation.setCreatedUpdatedDate(createUpdateDate);
userOrganisation.setAction(portalUser.getAction());
userOrganisation.setStatus(portalUser.getStatus());
userOrganisation.setActive(true);
portalUser.getPortalUserOrganisation().add(userOrganisation);
UserRole userRole = null;
for (RoleLookupDto dto : portalUserDto.getUserRoles())
userRole = new UserRole();
userRole.setPortalUser(portalUser);
userRole.setRoleLookup(roleLookupRepository.findOne(dto.getId()));
userRole.setCreatedUpdatedBy(context.getName());
userRole.setCreatedUpdatedDate(createUpdateDate);
userRole.setAction(portalUser.getAction());
userRole.setStatus(portalUser.getStatus());
userRole.setActive(true);
portalUser.getUserRole().add(userRole);
portalUser.setActive(false);
portalUser = portalUserRepository.save(portalUser);
return portalUser;
我看了这么多帖子,但这并没有解决我的问题。任何帮助表示赞赏。这里的 RoleLookup 是一个静态表。这是一个例外:
org.hibernate.TransientObjectException: object references an unsaved transient instance - 在刷新之前保存瞬态实例:com.commerzbank.clearing.ccp.domain.UserRole.roleLookup -> com.commerzbank.clearing.ccp.domain.RoleLookup;嵌套异常是 java.lang.IllegalStateException:org.hibernate.TransientObjectException:对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.commerzbank.clearing.ccp.domain.UserRole.roleLookup -> com.commerzbank.clearing。 ccp.domain.RoleLookup
【问题讨论】:
【参考方案1】:您应该为多对一设置cascade = "save-update "
。
【讨论】:
以上是关于对象引用了一个未保存的瞬态实例 - Spring,JPA Hibernate的主要内容,如果未能解决你的问题,请参考以下文章
HIbernate - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例
数据未保存:对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例 [重复]
TransientPropertyValueException:对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例
TransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例
org.hibernate.TransientObjectException:对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例