[原创]hibernate 一对一实践 by hjack(2)

2008-02-23 09:29:46来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


private int user_id;

/** full constructor */
public Topic(java.lang.String name, int user_id) {
this.name = name;
this.user_id = user_id;
}

/** default constructor */
public Topic() {
}

public int getId() {
return this.id;
}

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

public java.lang.String getName() {
return this.name;
}

public void setName(java.lang.String name) {
this.name = name;
}

public int getUser_id() {
return this.user_id;
}

public void setUser_id(int user_id) {
this.user_id = user_id;
}

public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof Topic) ) return false;
Topic castOther = (Topic) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}

}

3、修改Topic.java文件。
找到 private int user_id;
修改成private Author author;
找到 构造函数public Topic(java.lang.String name, int user_id),把参数int user_id改为Author author, 把函数里的this.user_id = user_id; 改为this.author = author;
找到以下两个函数
public int getUser_id() {
return this.user_id;
}

public void setUser_id(int user_id) {
this.user_id = user_id;
}
修改为
public Author getAuthor() {
return author;
}

public void setAuthor(Author author) {
this.author = author;
}
然后保存。以上文件保存在model包里。

4、修改Topic.hbm.xml文件。
删除下面这行
<property column="user_id" length="11" name="user_id" type="integer"/>
在</class>前添回<many-to-one>项如下
<many-to-one name="author" class="model.Author" column="user_id" unique="true"/>

通过以上操作就建立了Topic表与Author表之间的单向一对一关系,因为本工程中只需要从主题表去了解作者的信息,所以只需要单向的一对一就可以完成了。

5、建立测试用例。
1)、新建test包,在test包内建立HibernateUtil类。
/*
* 创建日期 2005-8-4
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package test;

/**
* @author hjack<br>
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/


import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory;
private static Configuration cfg = null;

static {
try {
cfg = new Configuration();
sessionFactory =cfg.configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException(
"Exception building SessionFactory: " ex.getMessage(),
ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}

hibernate.cfg.xml文件内容如下:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<!--<property name="connection.datasource">java:comp/env/JDBC/mysql</property>-->

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:osworkflow api 之 util

下一篇:SQL 中 Delete、Truncate、Drop