Java编程

  • IOS开发
  • android开发
  • PHP编程
  • JavaScript
  • ASP.NET
  • ASP编程
  • JSP编程
  • Java编程
  • 易语言
  • Ruby编程
  • Perl编程
  • AJAX
  • 正则表达式
  • C语言
  • 编程开发

    Hibernate的一对一关系

    2018-05-16 07:55:44 次阅读 稿源:互联网
    非官媒广告

      一对一关系的维护有两种,分为主键关联和唯一外键关联

      一对一关系的维护有两种,分为主键关联和唯一外键关联

      主键关联:(双向)

      

    (主控方)
    Person.java
    Person {
    private String username;
    //对被控方的引用
    private Address address;
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    public Address getAddress() {
    return address;
    }
    public void setAddress(Address address) {
    this.address= address;
    }
    }
    (被动方)
    Address.java
    Address{
    private String addressName;
    //对主控方的引用
    private Person person;
    public String getAddressName() {
    return addressName;
    }
    public void setAddressName(String addressName) {
    this.addressName= addressName;
    }
    public Person getPerson() {
    return person;
    }
    public void setPerson(Person person) {
    this.person= person;
    }
    public String toString() {
    return new ToStringBuilder(this).
    append("addressName", addressName).
    toString();
    }
    public boolean equals(Object o) {
    if ( !(o instanceof Address) {
    return false;
    }
    Address rhs = (Address) o;
    return new EqualsBuilder()
    .appendSuper(super.equals(o))
    .append(this.addressName, rhs.getAddressName())
    .isEquals();
    }
    public int hashCode() {
    return new HashCodeBuilder(17, 37).
    append(addressName).
    toHashCode();
    }
    }
    mapping 文件
    <class name="Person" table="person">
    <id name="id" type="long" column="id">
    <generator class="native"/>
    </id>
    <property name="username" column="username" type="string"/>
    <one-to-one
    name="address"
    class="Address"
    cascade="all"
    constrained="false"
    />
    </class>
    <class name="Address" table="Address" >
    <id name="id" column="id">
    <generator class="foreign">
    <param name="property">person</param>
    </generator>
    </id>
    <one-to-one name="person" class="Person" constrained="true"/>
    <property name="addressName" column="addressName" type="string"/>
    </class>

    非官媒部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与非官媒进行文章共享合作。

    非官媒广告
    非官媒广告
    非官媒广告