我在另一个Entity类中引用一个Entity并收到此错误。下面是示例代码。我在persistence.xml中也有这些类。
是什么原因导致此问题?我正在使用Spring数据JPA和Hibernate。
import javax.persistence.*;
@Entity
@Table(name = "users", schema = "university")
public class UsersEntity {
private long id;
@JoinColumn(name = "address_id", nullable = false)
private Address address;
@Id
@Column(name = "id")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
import javax.persistence.*;
@Entity
@Table(name = "address", schema = "university")
public class AddressEntity {
private long id;
private String street;
@Id
@Column(name = "id")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "street")
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
答案 0 :(得分:0)
因此,请尝试以下操作:
@Entity
@Table(name = "users", schema = "university")
public class UsersEntity {
private Long id;
private AddressEntity address;
@Id
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@OneToOne
@JoinColumn(name = "address_id", nullable = false)
public AddressEntity getAddress() {
return address;
}
public void setAddress(AddressEntity address) {
this.address = address;
}
}
@Entity
@Table(name = "address", schema = "university")
public class AddressEntity {
private Long id;
private String street;
@Id
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "street")
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
基本上,我所做的是替换Long的long。并添加了@OneToOne 我删除了@Basic,因为它是可选的。我相信它应该可以正常工作
答案 1 :(得分:0)
您错过了实体之间的关系类型。例如。您可以将单向@OneToOne 定义为
//...
public class UsersEntity {
//..
@OneToOne(mappedBy = "ugdArea", fetch = FetchType.LAZY)
@JoinColumn(name = "address_id", nullable = false)
private Address address;
//...
}