object references an unsaved transient instance - Spring, JPA Hibernate

扶醉桌前 提交于 2020-05-13 06:53:27

问题


Here is the code:

@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;

I have see so many post, but this has not solved my issue. Any help is appreciated. Here the RoleLookup is a static table. Here is the exception:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.commerzbank.clearing.ccp.domain.UserRole.roleLookup -> com.commerzbank.clearing.ccp.domain.RoleLookup; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.commerzbank.clearing.ccp.domain.UserRole.roleLookup -> com.commerzbank.clearing.ccp.domain.RoleLookup


回答1:


You should set a cascade = "save-update " for many-to-one side.



来源:https://stackoverflow.com/questions/18277986/object-references-an-unsaved-transient-instance-spring-jpa-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!