我正在 JPA 中尝试一对一的映射,在这里我建立了学生和联系人之间的关系,每个学生都有一个联系人。
我创建学生实体如下,
@Entity
@Table(name="TBL_STUDENT")
public class Student implements Serializable{
public Student(){ }
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Integer studentId;
@OneToOne(targetEntity=StudentContact.class,fetch=FetchType.LAZY)
@JoinColumn(name="CONTACT_ID")
private StudentContact contact;
....
....
....
}
现在 StudentContact 实体如下,
@Entity
@Table(name="TBL_STD_CONTACT")
public class StudentContact extends Serializable{
public StudentContact(){ }
@Id
@Column(name="ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer contactId;
...
...
// all the properties mapped,
public static class Builder{
private Integer contactId;
private String phoneNo;
private String streetAddr;
....
// all the properties as same as StudentContact
public Builder(String val){
this.city = val;
}
public Builder setContactId(Integer contactId) {
this.contactId = contactId;
return this;
}
// rest all the setter methods are like the above, having return type Builder
public StudentContact build(){
return new StudentContact(this);
}
}
private StudentContact(Builder builder){
this.contactId = builder.contactId;
this.city = builder.city;
this.phoneNo = builder.phoneNo;
.......
...
}
}
在上面的 StudentContact 实体中,您可以看到我创建了一个内部类 Builder,其职责是使用其“build”方法构建 StudentContact 对象,您可以在下面提到的 StudentTest 类中看到
现在我写了一个 StudentTest 类,它的主要方法如下,
public class StudentTest {
public static void main(String [] args){
try{
StudentDAO dao = new StudentDAO();
Student student = dao.getEntity(110);
StudentContact contact = new StudentContact.Builder("Bhubaneshwar")
.setPhoneNo("9867342313")
.setPinCode("400392")
.setState("Odhisha").build();
student.setContact(contact);
dao.updateEntity(student);
}catch(Exception e){
e.printStackTrace();
}
}
当我从 netbeans IDE 运行 StudentTest 时,它给出错误
Exception in thread "main" java.lang.VerifyError: Constructor must call super() or this() before return in method com.entities.StudentContact.<init>()V at offset 0
我无法理解这个错误,这个错误是否是因为我在 StudentContact 类中创建的内部类,
我该如何解决这个问题,