我尝试在两个表之间创建一对一的关系。
其中之一是Person:
public class Person implements Serializable {
static final long serialVersionUID = 1L;
private long id;
private String _email;
private String _pass;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmail() {
return _email;
}
public void set_email(String _email) {
this._email = _email;
}
public String getPass() {
return _pass;
}
public void set_pass(String _pass) {
this._pass = _pass;
}
}
第二个是ReqC2dmRegId表:
public class ReqC2dmRegId implements Serializable {
private static final long serialVersionUID = 1L;
Person person;
String C2dmid;
private long id;
public ReqC2dmRegId(){}
public String getC2dmid() {
return C2dmid;
}
public void setC2dmid(String c2dmid) {
C2dmid = c2dmid;
}
public ReqC2dmRegId(Person person, String C2dmid) {
super();
this.person = person;
this.C2dmid = C2dmid;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
现在,在我的程序中,我总是首先创建
Person
,并且仅在需要时添加此ReqC2dmRegId
。现在,我想做的就是链接这两个表。我的意思是,当我坚持使用此
ReqC2dmRegId
(当然,我向ReqC2dmRegId
中的人添加了正确的ID)时,我希望我的ReqC2dmRegId
用正确的Person
ID更新或保存新行。这些是我的hbm文件:
ReqC2dmRegId.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 26, 2012 11:29:57 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="c2dm.ReqC2dmRegId" table="REQC2DMREGID">
<id name="id" type="long">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" class="Entities.Person" cascade="all" />
<property name="C2dmid" type="java.lang.String">
<column name="C2DMID" />
</property>
</class>
</hibernate-mapping>
Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 26, 2012 11:29:57 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="Entities.Person" table="PERSON">
<id name="id" type="long">
<column name="ID" />
<generator class="increment" />
</id>
<property name="_email" type="java.lang.String" access="field">
<column name="_EMAIL" />
</property>
<property name="_pass" type="java.lang.String" access="field">
<column name="_PASS" />
</property>
</class>
</hibernate-mapping>
我究竟做错了什么?
当我尝试运行时:
//this should to update or save the object in DB
public void update (Object query){
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
//em.createNativeQuery(query).executeUpdate();
em.merge(query);
em.flush();
em.getTransaction().commit();
em.close();
}
我得到:
attempted to assign id from null one-to-one property:Person
最后,它应如下所示:
人
**id email _pass**
2 [email protected] 1234
ReqC2dmRegId
**id REQC2DMREGID**
2 ffgghhjj
更新:
在我放弃尝试了解它不起作用的方式之后
我更改了我的ReqC2dmRegId.hbm.xml
看起来像这样(多对一):
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Mar 27, 2012 9:58:08 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping>
<class name="c2dm.ReqC2dmRegId" table="REQC2DMREGID">
<id name="id" type="long">
<column name="ID" />
<generator class="identity" />
</id>
<many-to-one name="person" class="Entities.Person" fetch="join" unique="true" cascade="save-update" not-null="true" >
<column name="PERSON" />
</many-to-one>
<property name="C2dmid" type="java.lang.String">
<column name="C2DMID" />
</property>
</class> </hibernate-mapping>
而且工作正常,问题是当我尝试修改ReqC2dmRegId表时
使用我的更新方法,它使用相同的personid创建一个now行
id person_id C2dmid
1 3 asd123
2 3 dfvghj
的方式它不更新右行创建一个新的尽管我使“多对一”属性为unique =“ true”?
提前致谢
最佳答案
您必须清楚这种关系:是一对一还是多对一?
看起来像是多对一的单向关系。
这是带有注释的示例:
@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String _email;
private String _pass;
//getters and setters
}
和另一类:
@Entity
@Table(name="ReqC2dmRegId")
public class ReqC2dmRegId {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
@JoinColumn(name = "PERSON_ID")
private Person person;
//getters and setters
}
关于java - 与 hibernate 一对一关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9891601/