当前位置: 首页 > 知识库问答 >
问题:

对象引用一个未保存的临时实例——Spring,JPA Hibernate

韩博厚
2023-03-14

下面是代码:

@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是一个静态表。以下是例外:

组织。冬眠TransientObjectException:对象引用未保存的临时实例-在刷新之前保存临时实例:com。德国商业银行。清理CCP领域用户角色。roleLookup-

共有1个答案

蓝昊天
2023-03-14

您应该为多对一设置一个cascade=“save update”

 类似资料: