【问题标题】:"Select all Documents from one content" hibernate relationship many to many“从一个内容中选择所有文档”休眠关系多对多
【发布时间】:2018-08-22 00:50:12
【问题描述】:

我有两个模型:

 @Entity    
    public class Documento implements java.io.Serializable {

        @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "appc_contenido_documento", catalog = "appcope", 
        joinColumns = {
    @JoinColumn(name = "id_documento", nullable = false, updatable = 
        false)}, inverseJoinColumns = {
    @JoinColumn(name = "id_contenido", nullable = false, updatable = 
        false)})
    private Set<Contenido> contenidos = new HashSet<Contenido>(0);

}

@Entity   
public class Contenido implements java.io.Serializable {

     @Id
     @GeneratedValue(strategy = IDENTITY)
     @Column(name = "id", unique = true, nullable = false)
     private Integer id;

     @ManyToMany(fetch = FetchType.LAZY)
     @JoinTable(name = "appc_contenido_documento", catalog = "appcope", 
         joinColumns = {
     @JoinColumn(name = "id_contenido", nullable = false, updatable = 
         false)}, inverseJoinColumns = {
     @JoinColumn(name = "id_documento", nullable = false, updatable = 
         false)})
     private Set<Documento> documentos = new HashSet<Documento>(0);

}

我没有找到这样的例子,只有我找到了关系一对多的例子

如何使用 CriteriaQuery 选择一个内容的所有文档?

【问题讨论】:

    标签: java hibernate orm hibernate-criteria


    【解决方案1】:

    将多对多关系简化为一对多通常是个好主意。这通常可以通过引入一个桥接实体来完成。也许值得在这里添加这样一个表。

    【讨论】:

    • 不修改当前实体的代码就没有办法了吗?
    • 您应该更改当前代码,因为它不是很灵活。我假设您想将内容映射到文档。您应该从 1 个文档可以有多个内容或多个类型的内容属于单个文档的角度来考虑它。
    猜你喜欢
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多