我想将班级地址映射到班级员工,我所做的事情如下所示。
我的员工班
@Entity(name = "EMPLOYEE")
public class Employee {
@Id @GeneratedValue
@Column(name="EMPLOYEEID", length =30)
int id;
public Employee(String string, String string2,String string3, String string4) {
this.name=string;
this.age=string2;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="NAME", length = 30)
String name;
@Column(name="AGE", length = 30)
String age;
@OneToOne(mappedBy = "employee", cascade = CascadeType.ALL)
Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
我的地址类是
@Entity(name="Address")
public class Address {
@Id
@Column(name="EMPLOYEEID", length =30)
@GenericGenerator(name="generator", strategy="foreign", parameters = @Parameter(name="property", value="employee"))
@GeneratedValue(generator="generator")
int id;
@Column(name="LINE1", length=30)
String line1;
@Column(name="LINE2", length=30)
String line2;
@Column(name="LINE3", length=30)
String line3;
public String getLine1() {
return line1;
}
public void setLine1(String line1) {
this.line1 = line1;
}
public String getLine2() {
return line2;
}
public void setLine2(String line2) {
this.line2 = line2;
}
public String getLine3() {
return line3;
}
public void setLine3(String line3) {
this.line3 = line3;
}
}
但是每当我试图执行它时,我都会收到错误
未知的mappedBy in:com.hibernatetest.company.Employee.address,引用的属性unknown:com.hibernatetest.company.Address.employee
我在这里做错了什么?
答案 0 :(得分:1)
您引用了Address
的不存在的属性:
@OneToOne(mappedBy = "employee", cascade = CascadeType.ALL)
Address address;
查看OneToOne.mappedBy()的文档:
(可选)拥有该关系的字段。此元素仅在关联的反向(非拥有)侧指定。
因此,您的代码声明Address
实体拥有拥有该关系的字段employee
。但它没有这样的财产。
可能您可以使用以下代码实现此属性(免责声明:我没有对其进行测试,而且我不知道它是否适合您的特定情况):
@OneToOne(optional=false)
@JoinColumn(name="EMPLOYEEID")
Employee employee;
public Employee getEmployee() {
return employee;
}
答案 1 :(得分:0)
我认为你的未知是因为你给了小写字母的员工,但没有在任何地方尝试将其改为
@OneToOne(mappedBy = "EMPLOYEE", cascade = CascadeType.ALL) here