Java 具有用于构建实体的内部类的实体
我正在JPA中尝试一对一映射, 这里我采取了学生和联系人的关系,每个学生都有一个联系人 我已经创建了学生实体,如下所示Java 具有用于构建实体的内部类的实体,java,hibernate,jpa,orm,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")
@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;
....
....
....
}
@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;
.......
...
}
}
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();
}
}
现在,StudentContact实体如下所示
@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;
....
....
....
}
@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;
.......
...
}
}
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();
}
}
在上面的StudentContact实体中,您可以看到我创建了一个内部类生成器,其职责是通过使用其“build”方法构建StudentContact对象,您可以在下面提到的StudentTest类中看到
现在我写了一个StudentTest类,主要方法如下:
@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;
....
....
....
}
@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;
.......
...
}
}
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
线程“main”java.lang.VerifyError中出现异常:构造函数必须调用super()或this(),然后才能在方法com.entities.StudentContact中返回。()V偏移量0
我无法理解这个错误,这个错误是否是因为我在StudentContact类中创建的内部类
如何解决这个问题,java.lang.VerifyError意味着字节码不正确。通常可以通过项目的全面清理/重建来修复。(我有时在包/类重命名或类从一个包移动到另一个包后看到它)
正如注释中提到的:
extends Serializable
不正确。(可能是您字节码问题的原因?即使我在StudentContact的私有构造函数中编写了super()或this(),它仍然会给出一个错误,您还有其他问题;StudentContact extends Serializable就是其中之一