与hibernate的一对一关系

时间:2012-03-27 14:29:07

标签: java hibernate

我尝试在两个表之间创建一对一的关系。

其中一人是人:

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      lala@gmail.com     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的现在行

id    person_id  C2dmid
1      3          asd123
2      3          dfvghj

它没有更新右侧行的方式创建一个新的,虽然我将“多对一”属性设为unique =“true”?

提前致谢

1 个答案:

答案 0 :(得分:0)

你必须清楚这种关系:是一对一还是多对一? 看起来像是多对一的单向关系。

以下是Annotations的示例:

@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

}