Hibernate注释一对一单向
为什么我在一对一hibernate映射示例中得到以下4条sql语句单向,对于简单hbm配置中的相同示例,我只得到2条insert语句 注释sql语句如下所示: 休眠:从PersonDetail persondeta中选择persondeta.id,persondeta.fatherName作为fatherName1,persondeta.motherName作为motherName1,PersonDetail persondeta.id= Hibernate:插入个人电子邮件、名字、姓氏、个人详细信息、id值?、?、?、?、 Hibernate:在PersonDetail中插入父名、母名、id值?、?、 Hibernate:更新人员集电子邮件=?,firstName=?,lastName=?,personDetail\u id=?其中id= 对于HBMSQL语句,如下所示 Hibernate:插入个人电子邮件、名字、姓氏、id值?、?、?、 Hibernate:在PersonDetail中插入父名、母名、id值?、?、 我的hbm配置如下 } 包com.patel.model 导入javax.persistence.Column 导入javax.persistence.Entity 导入javax.persistence.Id @实体 公共类个人信息{Hibernate注释一对一单向,hibernate,annotations,Hibernate,Annotations,为什么我在一对一hibernate映射示例中得到以下4条sql语句单向,对于简单hbm配置中的相同示例,我只得到2条insert语句 注释sql语句如下所示: 休眠:从PersonDetail persondeta中选择persondeta.id,persondeta.fatherName作为fatherName1,persondeta.motherName作为motherName1,PersonDetail persondeta.id= Hibernate:插入个人电子邮件、名字、姓氏、个人详
@Id
private int id;
@Column
private String fatherName;
@Column
private String motherName;
public PersonDetail() {
super();
}
public PersonDetail(int id, String fatherName, String motherName) {
super();
this.id = id;
this.fatherName = fatherName;
this.motherName = motherName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public String getMotherName() {
return motherName;
}
public void setMotherName(String motherName) {
this.motherName = motherName;
}
@Override
public String toString() {
return "PersonDetail [id=" + id + ", fatherName=" + fatherName
+ ", motherName=" + motherName + "]";
}
}
下面是导致查询的代码
package com.patel.client
导入org.hibernate.Session
导入org.hibernate.SessionFactory
导入org.hibernate.Transaction
导入org.hibernate.TransactionException
导入org.hibernate.cfg.AnnotationConfiguration
导入com.patel.model.Person
导入com.patel.model.PersonDetail
公共类JpaClient{
public static void main(String[] args) {
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Person p = new Person(1, "Bhupati", "Patel", "xyz@xyz.com");
PersonDetail pd = new PersonDetail(1, "Bhupatis's Father", "Bhupati's Mother");
p.setPersonDetail(pd);
session.save(p);
session.save(pd);
tx.commit();
System.out.println("Inserted");
} catch (TransactionException e) {
e.printStackTrace();
tx.rollback();
}
}
}
在hbm文件的客户端代码中:
SessionFactory sf=new Configuration.configure.buildSessionFactory
其余的都一样
使用哪种注释配置,我将得到两条insert语句作为hbm配置?导致执行这些查询的代码在哪里?我刚刚添加了它。尝试将optional=false添加到OneTONE注释中。添加后会出现以下错误,我正在粘贴其中的一部分Hibernate:select personeda_uu2;id,PersonDetail PersonDetail persondeta_uuu中的persondeta.fatherName作为fatherName1_u,persondeta_uu.motherName作为motherName1_u,PersonDetail persondeta.id=?线程主org.hibernate.PropertyValueException中的异常:not null属性引用空值或临时值:com.patel.model.Person.PersondetailPersondetailPersondetail必须首先保存PersonDetail,因为此人引用了它。
@Id
private int id;
@Column
private String fatherName;
@Column
private String motherName;
public PersonDetail() {
super();
}
public PersonDetail(int id, String fatherName, String motherName) {
super();
this.id = id;
this.fatherName = fatherName;
this.motherName = motherName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public String getMotherName() {
return motherName;
}
public void setMotherName(String motherName) {
this.motherName = motherName;
}
@Override
public String toString() {
return "PersonDetail [id=" + id + ", fatherName=" + fatherName
+ ", motherName=" + motherName + "]";
}
public static void main(String[] args) {
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Person p = new Person(1, "Bhupati", "Patel", "xyz@xyz.com");
PersonDetail pd = new PersonDetail(1, "Bhupatis's Father", "Bhupati's Mother");
p.setPersonDetail(pd);
session.save(p);
session.save(pd);
tx.commit();
System.out.println("Inserted");
} catch (TransactionException e) {
e.printStackTrace();
tx.rollback();
}
}