Spring和JPA 2.0-与额外的列建立多对多关系的复合键

我有多对多关系中的两个实体Foo和Bar。加入实体是FooBar,并且由于该实体具有另一个属性(其自己的ID),因此我在所有者端(@ManyToOne)和FooBar的依赖实体中使用了@OneToManyFooBar)。如何创建扩展FooBarRepository的{​​{1}},而在CrudRepository中没有显式的复合键字段?理想情况下,我不想更改我的FooBar类的成员。

我尝试使用FooBar,但是我不想在@IdClass内使用字段fooIdbarId,但出现了这个异常:

FooBar

我还尝试明确遵循Caused by: org.hibernate.AnnotationException: Property of @IdClass not found in entity com.nano.testers.test.FooBar: barId的文档并按名称明确引用列,但是我失败了(也许解决方案在这里?)

  

主键类中的字段或属性的名称与实体的主键字段或属性必须对应,并且它们的类型必须相同。

我试图将IdClassFoo内的字段名称更改为Bar,以便在它们中将它们分别引用为idfoo_id连接表,但例外是相同的。

我不想使用bar_id,如果这意味着我需要在@EmbeddedId类内使用FooBarPk类型的字段。

FooBar
@Entity
public class Foo {
    @Id
    private Long fooId;

    @OneToMany(mappedBy = "foo",cascade = CascadeType.ALL)
    private Set<FooBar> foobars;
}
@Entity
public class Bar {
    @Id
    private Long barId;

    @OneToMany(mappedBy = "bar",cascade = CascadeType.ALL)
    private Set<FooBar> foobars;
}
@Entity
//@IdClass(FooBarPk.class)
public class FooBar implements Serializable {
    @Id
    private Long fooBarId;

    @Id
    @ManyToOne
    @JoinColumn
    private Foo foo;

    @Id
    @ManyToOne
    @JoinColumn
    private Bar bar;

}
public class FooBarPk implements Serializable {
    private Long fooId;
    private Long barId;
    private Long fooBarId;
}
rayest 回答:Spring和JPA 2.0-与额外的列建立多对多关系的复合键

看起来组合键类中的字段名称必须与所引用实体的名称相同。我认为这些名称没有遵循干净的代码原则,但是我现在必须使用此解决方案。

public class FooBarPk implements Serializable {
    private Long foo;
    private Long bar;
    private Long fooBarId;
}
本文链接:https://www.f2er.com/3145243.html

大家都在问