4

这是代码:

@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:对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例: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

4

1 回答 1

2

您应该cascade = "save-update "为多对一设置一个。

于 2016-10-26T11:55:22.777 回答