0

我正在通过 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>
4

1 回答 1

0

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

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

于 2013-03-01T07:09:39.610 回答