引用瞬态实体时Hibernate TransientObjectException

时间:2017-02-10 10:51:54

标签: java hibernate jpa spring-data spring-data-jpa

@Entity
@Table(name = "PE_HOME")
@Cacheable
public class Home extends DefEntity implements IHome {

    private static final long serialVersionUID = 1L;

    @Column(name = "DESCRIPTION", nullable = true, unique = false)
    private String description = null;

    @Column(name = "INDICE", nullable = true, unique = false)
    private Integer indice = null;

    public Home() {
        super();
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getIndice() {
        return this.indice;
    }

    public void setIndice(Integer indice) {
        this.indice = indice;
    }
}


@Entity
@Table(name = "PE_PERSON")
public class Person extends DefEntity implements IPerson {

    private static final long serialVersionUID = 1L;

    @OneToOne(fetch = FetchType.LAZY, targetEntity = Home.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, orphanRemoval = true)
    @JoinColumn(name = "ID_HOME", nullable = true, unique = false)
    private IHome home = null;

    public Person() {
        super();
    }

    public IHome getHome() {
        return this.home;
    }

    public void setHome(IHome home) {
        this.home = home;
    }
}

@Entity
@Table(name = "PE_FAMILY")
@Cacheable
public class Family extends DefEntity implements IFamily {

    private static final long serialVersionUID = 1L;

    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @OneToMany(fetch = FetchType.LAZY, targetEntity = Person.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, orphanRemoval = true)
    @OrderColumn(name = "LIST_INDEX", nullable = false)
    @JoinTable(name = "PE_FAMILY_PERSONS", joinColumns = { @JoinColumn(name = "ID_FAMILY") }, inverseJoinColumns = { @JoinColumn(name = "ID_PERSON") }, uniqueConstraints = @UniqueConstraint(columnNames = {"ID_PERSON", "LIST_INDEX" }))
    private List<IPerson> persons = new ArrayList<IPerson>();

    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @OneToOne(fetch = FetchType.LAZY, targetEntity = FamilyDetails.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, orphanRemoval = true)
    @JoinColumn(name = "ID_FAM_DET", nullable = true, unique = true)
    private IFamilyDetails familyDetails = null;

    public Family() {
        super();
    }

    public List<IPerson> getPersons() {
        return this.persons;
    }

    public void setPersons(List<IPerson> persons) {
        this.persons = persons;
    }

    public IFamilyDetails getFamilyDetails() {
        return this.familyDetails;
    }

    public void setFamilyDetails(IFamilyDetails familyDetails) {
        this.familyDetails = familyDetails;
    }
}


@Entity
@Table(name = "PE_FAMILY_DETAILS")
@Cacheable
public class FamilyDetails extends DefEntity implements IFamilyDetails {

    private static final long serialVersionUID = 1L;

    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = Home.class)
    @JoinTable(name = "AX_FAM_DET_HOMES", joinColumns = { @JoinColumn(name = "ID_FAM_DET") }, inverseJoinColumns = { @JoinColumn(name = "ID_HOME") })
    private Set<IHome> homes = new HashSet<IHome>();

    public FamilyDetails() {
        super();
    }

    public Set<IHome> getHomes() {
        return this.homes;
    }

    public void setHomes(Set<IHome> homes) {
        this.homes = homes;
    }
}

public class FamilyManager {

    public void saveFamily(final IFamily family) {
        updateFamilyDetails(family);
        familyDao.save(family);
    }

    private void updateFamilyDetails(final IFamily family) {
        IFamilyDetails details = family.getFamilyDetails();
        if (details == null) {
            details = new FamilyDetails();
            family.setFamilyDetails(details);
        }
        details.getHomes().clear();
        for (IPerson p : family.getPersons()) {
            if (p.getHome() != null) {
                details.getHomes().add(p.getHome());
            }
        }
    }
}

因此,Home实体与Person实体一起持久存在,并与Family实体一起持久化。 FamilyDetails实体也与Family实体一起保留。在持久化Family实体之前,我试图从FamilyDetails更新homes属性。问题是当我将它们添加到集合中时,添加的房屋尚未保留。我相信当他们与家人的实体一起坚持下去时,他们也会在FamilyDetails的家庭收藏中更新,但我得到了

org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.model.impl.Home;

有没有办法避免这种情况?

1 个答案:

答案 0 :(得分:0)

很明显:在保存Home之前保存FamilyDetail,这可以通过添加带有@ManyToMany属性的cascade注释来实现,如同在他的评论。

考虑一下:没有配置任何级联行为就像说:我负责持久化。 JPA为什么要注意保持它们并以正确的顺序保持它们,因为这是在其他地方相对于其他实体配置的。