私はJPAで1対1のマッピングを試しています。ここでは、StudentとContactの関係を取得しました。各学生には、連絡先があります。
次のようにStudentエンティティを作成しました。
@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を作成したことがわかります。このクラスの責任は、以下で説明するStudentTestクラスで確認できる「build」メソッドを使用してStudentContactオブジェクトを構築することです。
これで、メインメソッドを持つ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クラスで作成した内部クラスが原因であるかどうかにかかわらず、このエラーを理解できません。
どうすればこれを解決できますか、