两个表中相同主键的一对一映射

时间:2013-03-01 07:03:02

标签: hibernate

我在xml方面经历了一对一的映射,通过在这两个表中使用相同主键的方法,我有以下pojo ..还请指出它是我在hbm中特别关注的正确方法如果我遗失了文件或其他内容请告诉我

1)第一个是人

public class Person
{
     private int personId;
     private String name;
     private Address address;
    public Person() 
    {
        super();
    }
    public Person(String name, Address address) 
    {
        super();
        this.name = name;
        this.address = address;
    }
    public int getPersonId() {
        return personId;
    }
    public void setPersonId(int personId) {
        this.personId = personId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }

}

2)另一个是地址

public class Address 
{
     private int id;
     private String city,state;

    public Address()
    {
        super();

    }

    public Address(String city, String state)
    {
        super();
        this.city = city;
        this.state = state;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }

}

和hbm xml映射文件是..

<class name="mypack.Address" table="address31">
<id name="id" column="addressId" type="int">
<generator class="increment"/>
</id>
<property name="city"/>
<property name="state"/>
</class>

<class name="mypack.Person" table="person31">
<id name="personId" type="int">
<generator class="foreign">
<param name="property">address</param>
</generator>
</id>
<property name="name"/>
<one-to-one name="address" class="mypack.Address"/>
</class>

</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

请参阅以下链接以供参考。可能会对您有所帮助

http://www.jcombat.com/hibernate/one-to-one-xml-mapping-with-primary-key-association