package net.sf.hibernate.examples.quickstart; public class Cat {
private String id;
private String name;
private char sex;
private float weight;
public Cat() {
}
public String getId() {
return id;
}
private void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
3) 将以下代码保存为O/R映射文件Cat.hbm.xml,放入%WebApp%/WEB-INF/classes
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
4) 在数据库内建表,结构如下
Column | Type | Modifiers
--------+-----------------------+-----------
cat_id | character(32) | not null
name | character varying(16) | not null
sex | character(1) |
weight | real |
Indexes: cat_pkey primary key btree (cat_id)
package net.sf.hibernate.examples.quickstart; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(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();
}
}
6) 将以下代码保存为test.jsp,放入%WebApp%/,用http测试
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="net.sf.hibernate.Transaction"%>
<%@ page import="net.sf.hibernate.Session"%>
<%@ page import="net.sf.hibernate.cfg.*"%>
<%@ page import="net.sf.hibernate.Query"%>
<%@ page import="net.sf.hibernate.examples.quickstart.HibernateUtil"%>
<%@ page import="net.sf.hibernate.examples.quickstart.Cat"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<%
//添加一只Cat
Session ses = HibernateUtil.currentSession();
Transaction tx= ses.beginTransaction();
Cat princess = new Cat();
princess.setName("王海利");
princess.setSex('F');
princess.setWeight(70.4f);
ses.save(princess);
tx.commit();
HibernateUtil.closeSession();
//读取库里所有Cat
ses = HibernateUtil.currentSession();
tx= ses.beginTransaction();
Query query = ses.createQuery("select c from Cat as c where c.sex = :sex");
query.setCharacter("sex", 'F');
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println("<br>Female Cat: " + cat.getName()+"+"+cat.getSex()+"+"+cat.getWeight());
}
tx.commit();
HibernateUtil.closeSession();
%>
</body>
</html>