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

对象引用未保存的瞬态实例在刷新错误之前保存瞬态实例

姬正文
2023-03-14

对象引用未保存的临时实例-在刷新之前保存临时实例。

我有一个2类组织实体和应用实体。组织和应用程序有多对多的关系。

组织实体

@ManyToMany(cascade = { CascadeType.PERSIST })
@JoinTable(
    name = "APP_ORG_REF",
    joinColumns = { @JoinColumn(referencedColumnName = "id") },
    inverseJoinColumns = { @JoinColumn(referencedColumnName = "id") })
private Set<ApplicationEntity> applications;

申请实体有

/**
 * Many to Many Relationship ref with Application.
 */
@ManyToMany(mappedBy = "applications", cascade = CascadeType.ALL)
private Set<OrganizationEntity> organizations;

组织运作impl。JAVA

public OrganizationEntity createOrganizationEntity (final String org, final String app,
    final String service) throws KMSCommonException
{
    log.debug("Entered createOrganizationEntity");
    createValidator(service, app, org, true).verify();
    final ApplicationEntity applicationEntity =
        applicationRepository.findByName(app, service);
    if (applicationEntity == null) {
        throw new KMSCommonException("Application doesn't exist.");
    }
    final OrgKeyRefEntity orgKeyRefEntity = OrgKeyRefEntity.builder().build();
    final OrganizationEntity organizationEntity = OrganizationEntity.builder()
        .name(org)
        .uuid(UUID.randomUUID().toString())
        .createdAt(new Timestamp(System.currentTimeMillis()))
        .build();
    addOrgAppRef(organizationEntity, applicationEntity);
    return organizationEntity;
}

        private void addOrgAppRef (final OrganizationEntity org, final ApplicationEntity 
             applicationEntity) {
    final Set<ApplicationEntity> applicationEntities = new HashSet<>();
    applicationEntities.add(applicationEntity);
    org.setApplications(applicationEntities);
    final Set<OrganizationEntity> organizationEntities = new HashSet<>();
    organizationEntities.add(org);
    applicationEntity.setOrganizations(organizationEntities);
    applicationRepository.save(applicationEntity);
}

我的问题是,我是否使用applicationRepository保存applicationEntity。save(applicationEntity)也会像我的cascade类型一样保存组织。所有的一切都在现场定义。我犯了这个错误。

原因:org。冬眠TransientPropertyValueException:对象引用未保存的临时实例-在刷新之前保存临时实例

共有1个答案

勾学博
2023-03-14

您正在从数据库获取应用程序信息。

final ApplicationEntity applicationEntity =
    applicationRepository.findByName(app, service);

你在用它来组织。因此,我建议在方法createOrganizationEntity上使用@Transactional注释。

如果没有解决,请分享完整的实体类,以便更好地理解。

 类似资料: