“从一个内容中选择所有文档”hibernate关系多对多

时间:2018-03-13 16:52:35

标签: java hibernate orm hibernate-criteria

我有两个模型:

 @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选择内容的所有文档?

1 个答案:

答案 0 :(得分:0)

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