Hibernate注释一对一单向

时间:2015-03-08 08:27:51

标签: hibernate annotations

为什么我在我的一对一hibernate映射示例(Uni-directional)中获得了跟随数字(4)的sql语句,在简单的hbm配置中,我只得到相同示例的2个插入语句。

对于注释sql语句如下:

Hibernate:从PersonDetail persondeta_中选择persondeta_.id,persondeta_.fatherName作为fatherName1_,persondeta_.motherName作为motherName1_,其中persondeta_.id =?

Hibernate:插入Person(email,firstName,lastName,personDetail_id,id)值(?,?,?,?,?)

Hibernate:插入PersonDetail(fatherName,motherName,id)值(?,?,?)

Hibernate:更新Person set email = ?, firstName = ?, lastName = ?, personDetail_id =?其中id =?

对于hbm sql语句如下

Hibernate:插入Person(email,firstName,lastName,id)值(?,?,?,?)

Hibernate:插入PersonDetail(fatherName,motherName,id)值(?,?,?)

我的hbm配置如下

<class name="com.patel.model.Person">
    <id name="id">
        <generator class="assigned" />
    </id>
    <property name="firstName" />
    <property name="lastName" />
    <property name="email" />
    <one-to-one name="personDetail" class="com.patel.model.Persondetail" />
</class>
<class name="com.patel.model.PersonDetail">
    <id name="id">
        <generator class="assigned" />
    </id>
    <property name="fatherName" />
    <property name="motherName" />
</class>

My Annotations类如下:

package com.patel.model;

导入javax.persistence.Column;

导入javax.persistence.Entity;

导入javax.persistence.Id;

导入javax.persistence.OneToOne;

@Entity

公共类人员{

@Id
private int id;
@Column
private String firstName;
@Column
private String lastName;
@Column
private String email;

@OneToOne
private PersonDetail personDetail;

public Person() {
    super();
}

public Person(int id, String firstName, String lastName, String email) {
    super();
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Override
public String toString() {
    return "Person [id=" + id + ", firstName=" + firstName + ", lastName="
            + lastName + ", email=" + email + "]";
}

public PersonDetail getPersonDetail() {
    return personDetail;
}

public void setPersonDetail(PersonDetail personDetail) {
    this.personDetail = personDetail;
}

}

package com.patel.model;

导入javax.persistence.Column;

导入javax.persistence.Entity;

导入javax.persistence.Id;

@Entity

public class 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 + "]";
}

}

以下是导致查询的代码

package com.patel.client;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.TransactionException;

import org.hibernate.cfg.AnnotationConfiguration;

import com.patel.model.Person;

import com.patel.model.PersonDetail;

public class 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();

剩下的是相同的

使用哪个注释配置我将获得两个插入语句作为hbm配置?

0 个答案:

没有答案