java - 具有用于构建实体的内部类的实体

标签 java hibernate jpa orm

我正在尝试在 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 类中创建的内部类,

我该如何解决这个问题,

最佳答案

java.lang.VerifyError 表示字节码不正确。通常可以通过完全清理/重建项目来修复它。 (我有时会在包/类重命名或类从一个包移动到另一个包后看到它)。

正如评论中提到的:extends Serialized 不正确。 (也许是你的字节码问题的原因?)

关于java - 具有用于构建实体的内部类的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14978963/

相关文章:

JavaFX 元素在动画之前对齐

java - hibernate pojo 过度暴露

java - 使用具有复杂条件的 JPA 实体图

java - Playframework:发生 JPA 错误(无法构建 EntityManagerFactory)

java - Sonal lint : change this condition so that it does not always evaluate to true

java - 使用 Apache POI 将父列添加到 Excel 数据透视表

java - Android:如何将 fragment 加载到 FrameLayout 中

java - 如何防止 Hibernate 在使用 session.saveOrUpdate(Object) 时抛出 ConstraintViolationException?

java - JPA ManyToMany 使用分组和交叉连接将数据连接在一起

java - 性能 : Checked and unchecked exceptions in JAVA