具有复合ID的IndexEmbedded属性的Hibernate Search Association

时间:2014-04-20 05:47:43

标签: java hibernate hibernate-search

当A类具有主键id1且B类具有由id1和id2组成的复合键时,如何将A类与B类关联。

代码将如下所示:

@Entity
@Indexed
public class ClassA {

    @Id
    @DocumentId
    private Integer id1;

    @Field
    private Integer description;

    @ManyToOne(cascade = {CascadeType.ALL})
    @IndexEmbedded
    ClassB b;

    // getters and setters

}


@Entity
public class ClassB {

    @EmbdeddedId
    @DocumentId
    private ClassB_Pk id;
}

@Embeddable
public class ClassB_Pk {

    private Integer id1;
    private Integer id2;

    // getters and setters

}

解决方案方法1:

public class classA {

    @ManyToOne(cascade = { CascadeType.ALL})
    @IndexedEmbedded
    @JoinColumn(name="id1")
    private ClassB b;

}

收到错误:Student_Main的列数错误。应该是2

解决方案方法2:

public class classA {

    @ManyToOne(cascade = { CascadeType.ALL})
    @IndexedEmbedded
    @MapsId("id1")
    private ClassB b;

}

错误:没有抛出错误,但没有发生索引(用luke检查)

我偶然发现了这个问题。任何建议将不胜感激。

提前谢谢

1 个答案:

答案 0 :(得分:1)

问题解决了。实际上,从A类到B类之间存在OneToMany关系。我改变了这些类之间的关系,并在它们两者上加上了“JoinColumn”注释。代码如下所示:

@Entity
@Indexed
public class ClassA {
    @Id
    @DocumentId
    private Integer id1;

    @Field
    private Integer description;

    @OneToMany(fetch=FetchType.EAGER, cascade = {CascadeType.ALL})
    @IndexEmbedded
    @JoinColumn(name="id1", referencedColumn="id1", updatable = false, insertable = false)
    Set<ClassB> b;

    // getters and setters

}

@Entity
public class ClassB {
    @EmbeddedId
    @DocumentId
    private ClassB_Pk id;

    @ContainedIn
    @ManyToOne(fetch=FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinColumn(name="id2", referencedColumnName="id2", insertable=false, updatable=false)
    ClassA a;


// other properties
// setters and getters

}

@Embeddable
public class ClassB_Pk {
    private Integer id1;
    private Integer id2;

    // getters and setters

}

希望这对任何偶然发现类似情况的人都有帮助。