[英]Map JPA Embedded entity class id to Embeddable entity class id
I have a class: 我有一节课:
@Entity
public class A {
@Embedded
@AttributeOverride(name = "id", column = @Column(name = "b_id"))
private B b;
}
There is column b_id BIGINT NOT NULL in table A 表A中有列b_id BIGINT NOT NULL
@Embeddable
@Entity
public class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
we are getting error: Caused by: org.hibernate.MappingException: component property not found: id 我们收到错误:引起:org.hibernate.MappingException:找不到组件属性:id
Basically, we need to map B in A using id 基本上,我们需要使用id在A中映射B.
Kindly help 请帮助
I think the problem is with @Id
in embedded class. 我认为问题在于嵌入式类中的
@Id
。 We can not use in an embedded class. 我们不能在嵌入式类中使用。 Try removing that?
尝试删除它? If you can remove it, try using
@EmbeddedId
if you just need an id field. 如果你可以删除它,如果你只需要一个id字段,请尝试使用
@EmbeddedId
。
Try this 尝试这个
@Entity
public class A implements Serializable {
private static final long serialVersionUID = 9154946919235019012L;
@Embedded
@AttributeOverride(name = "id", column = @Column(name = "b_id"))
private B b;
public A() {
}
public A(B b) {
this.b = b;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
And here is class B 这是B级
@Embeddable
@Entity
public class B implements Serializable {
private static final long serialVersionUID = 5579181803793008928L;
@Id
@Column(nullable = false)
private Long id;
public B(Long id) {
this.id=id;
}
public B(){
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
You don't have getters and setters, or an additional constructor besides the implicit no arg one. 除了隐式no arg之外,你没有getter和setter,或者没有其他构造函数。 You should have both a no-args constructor and getter and setter methods.
你应该有一个no-args构造函数和getter和setter方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.