休眠OneToOne和OneToMany

时间:2019-07-10 11:25:17

标签: java hibernate jpa

我在使用Hibernate / JPA的Spring中使用正确的注释时遇到问题。 我有一个实体(BaseModel),并且该实体具有相同实体的子代。 这样我就可以做到:

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, 
property = "id", resolver = EntityIdResolver.class, scope = BaseModel.class)
public class BaseModel {

    @Id
    @Type(type="org.hibernate.type.UUIDBinaryType")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "treeSettings_id", referencedColumnName = "id")
    private TreeSettings treeSettings = new TreeSettings();
....
}

@Entity
public class TreeSettings {

    @Id
    @Type(type="org.hibernate.type.UUIDBinaryType")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;

    @OneToOne(mappedBy = "treeSettings", cascade = CascadeType.ALL)
    @JsonIdentityReference(alwaysAsId = true)
    private BaseModel baseModel;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, orphanRemoval = true)
    @JoinColumn(name = "treeChildList_id", referencedColumnName = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private Set<BaseModel> treeChildList = new HashSet<>();
....
}

这些子项位于TreeSettings中,因为我需要更多属性来稍后添加。但是现在我找不到正确的注释。这是我尝试过的方法,但是似乎没有用。

{{1}}

0 个答案:

没有答案