Hibernate中one-to-one

(请将此图片下复制到本地,重命名为"po.rar",然后解压,之后使用Myeclipse工具导入,数据库脚本请在SQL Server上运行)

(1)数据库DDL

#
# Table structure for table 'author'
#

CREATE TABLE author (
author_id char(20) NOT NULL default '',
person_id char(20) default NULL,
PRIMARY KEY (author_id)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

#
# Table structure for table 'person'
#

CREATE TABLE person (
person_id char(20) NOT NULL default '',
name char(20) default NULL,
PRIMARY KEY (person_id)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

(2)映射文件

<!-- Author.hbm.xml -> <hibernate-mapping package="po"> <class name="Author" table="author"> <id name="authorId" column="author_id" type="java.lang.String"> <generator class="assigned" /> </id> <many-to-one cascade="all" class="po.Person" column="person_id" name="person" not-null="true" outer-join="auto" unique="true" /> </class> </hibernate-mapping> <!-- Person.hbm.xml -> <hibernate-mapping package="po"> <class name="Person" table="person"> <id name="personId" column="person_id" type="java.lang.String"> <generator class="assigned" /> </id> <property name="name" column="name" type="java.lang.String" /> <one-to-one name="author" class="Author" property-ref="person" /> </class> </hibernate-mapping>

(3)POJO文件

AbstractAuthor.java

package po; import java.io.Serializable; public abstract class AbstractAuthor implements Serializable { /** * The cached hash code value for this instance. Settting to 0 triggers * re-calculation. */ private int hashValue = 0; /** The composite primary key value. */ private java.lang.String authorId; /** The value of the simple personId property. */ private java.lang.String personId; /** * Simple constructor of AbstractAuthor instances. */ public AbstractAuthor() { } /** * Constructor of AbstractAuthor instances given a simple primary key. * * @param authorId */ public AbstractAuthor(java.lang.String authorId) { this.setAuthorId(authorId); } /** * Return the simple primary key value that identifies this object. * * @return java.lang.String */ public java.lang.String getAuthorId() { return authorId; } /** * Set the simple primary key value that identifies this object. * * @param authorId */ public void setAuthorId(java.lang.String authorId) { this.hashValue = 0; this.authorId = authorId; } /** * Return the value of the person_id column. * * @return java.lang.String */ public java.lang.String getPersonId() { return this.personId; } /** * Set the value of the person_id column. * * @param personId */ public void setPersonId(java.lang.String personId) { this.personId = personId; } /** * Implementation of the equals comparison on the basis of equality of the * primary key values. * * @param rhs * @return boolean */ public boolean equals(Object rhs) { if (rhs == null) return false; if (!(rhs instanceof Author)) return false; Author that = (Author) rhs; if (this.getAuthorId() != null && that.getAuthorId() != null) { if (!this.getAuthorId().equals(that.getAuthorId())) { return false; } } return true; } /** * Implementation of the hashCode method conforming to the Bloch pattern * with the exception of array properties (these are very unlikely primary * key types). * * @return int */ public int hashCode() { if (this.hashValue == 0) { int result = 17; int authorIdValue = this.getAuthorId() == null ? 0 : this .getAuthorId().hashCode(); result = result * 37 + authorIdValue; this.hashValue = result; } return this.hashValue; } }

Author.java
package po; import java.io.Serializable; public class Author extends AbstractAuthor implements Serializable { private Person person; /** * Simple constructor of Author instances. */ public Author() { } /** * Constructor of Author instances given a simple primary key. * * @param authorId */ public Author(java.lang.String authorId) { super(authorId); } /* Add customized code below */ public void setPerson(Person person) { this.person = person; } public Person getPerson() { return person; } }

AbstractPerson.java

package po; import java.io.Serializable; public abstract class AbstractPerson implements Serializable { /** * The cached hash code value for this instance. Settting to 0 triggers * re-calculation. */ private int hashValue = 0; /** The composite primary key value. */ private java.lang.String personId; /** The value of the simple name property. */ private java.lang.String name; /** * Simple constructor of AbstractPerson instances. */ public AbstractPerson() { } /** * Constructor of AbstractPerson instances given a simple primary key. * * @param personId */ public AbstractPerson(java.lang.String personId) { this.setPersonId(personId); } /** * Return the simple primary key value that identifies this object. * * @return java.lang.String */ public java.lang.String getPersonId() { return personId; } /** * Set the simple primary key value that identifies this object. * * @param personId */ public void setPersonId(java.lang.String personId) { this.hashValue = 0; this.personId = personId; } /** * Return the value of the name column. * * @return java.lang.String */ public java.lang.String getName() { return this.name; } /** * Set the value of the name column. * * @param name */ public void setName(java.lang.String name) { this.name = name; } /** * Implementation of the equals comparison on the basis of equality of the * primary key values. * * @param rhs * @return boolean */ public boolean equals(Object rhs) { if (rhs == null) return false; if (!(rhs instanceof Person)) return false; Person that = (Person) rhs; if (this.getPersonId() != null && that.getPersonId() != null) { if (!this.getPersonId().equals(that.getPersonId())) { return false; } } return true; } /** * Implementation of the hashCode method conforming to the Bloch pattern * with the exception of array properties (these are very unlikely primary * key types). * * @return int */ public int hashCode() { if (this.hashValue == 0) { int result = 17; int personIdValue = this.getPersonId() == null ? 0 : this .getPersonId().hashCode(); result = result * 37 + personIdValue; this.hashValue = result; } return this.hashValue; } }

Person.java

package po; import java.io.Serializable; public class Person extends AbstractPerson implements Serializable { private Author author; /** * Simple constructor of Person instances. */ public Person() { } /** * Constructor of Person instances given a simple primary key. * * @param personId */ public Person(java.lang.String personId) { super(personId); } /* Add customized code below */ public void setAuthor(Author author) { this.author = author; } public Author getAuthor() { return author; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值