我有多对多关系中的两个实体Foo和Bar。加入实体是FooBar
,并且由于该实体具有另一个属性(其自己的ID),因此我在所有者端(@ManyToOne
)和FooBar
的依赖实体中使用了@OneToMany
(Foo
和Bar
)。如何创建扩展FooBarRepository
的{{1}},而在CrudRepository
中没有显式的复合键字段?理想情况下,我不想更改我的FooBar
类的成员。
我尝试使用FooBar
,但是我不想在@IdClass
内使用字段fooId
和barId
,但出现了这个异常:
FooBar
我还尝试明确遵循Caused by: org.hibernate.AnnotationException: Property of @IdClass not found in entity com.nano.testers.test.FooBar: barId
的文档并按名称明确引用列,但是我失败了(也许解决方案在这里?)
主键类中的字段或属性的名称与实体的主键字段或属性必须对应,并且它们的类型必须相同。
我试图将IdClass
和Foo
内的字段名称更改为Bar
,以便在它们中将它们分别引用为id
和foo_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;
}