2013-02-20 130 views
0

我嘗試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實體,你可以看到我已經創建了一個內部類生成器,其責任是通過使用其「構建」方法來構建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(); 
      } 
} 

當我運行StudentTest從NetBeans IDE中,它給作爲錯誤

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類已經創建了內部類,

我怎樣才能解決這個問題,

+0

即使我在我的私人構造函數StudentContact中寫入super()或this()仍然會給出錯誤 – 2013-02-20 11:47:18

+2

您還有其他問題; StudentContact _extends_ Serializable是一個 – 2013-02-20 11:49:21

回答

0

java.lang.VerifyError指byteco德是不正確的。通常它可以通過完全清理/重建項目來解決。 (我有時會在包/班級重命名後看到它,或者從一個包到另一個包的類)。

正如評論中提到的:extends Serializable是不正確的。 (也許是你的字節碼問題的原因?)