Hibernat试用一
时间:2009-12-13 51CTO.COM
HIBERNATE是一个数据库层持久框架,今天下载了一个3.3.1版试用了一下;
首先创建一个对象类:
import java.util.Date;
public class Event {
private Long id;
private String title;
private Date date;
public Event() {
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
接下来创建相应对象的XML配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native" />
</id>
<property name="date" type="timestamp" column="EVENT_DATE" />
<property name="title" />
</class>
</hibernate-mapping>
Hibernat试用一(2)
时间:2009-12-13 51CTO.COM
接下来就是创建Hibernate的配置文件hibernate.cfg.xml:
<?xml version=''1.0'' encoding=''utf-8''?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate''s automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!--
Drop and re-create the database schema on startup <property
name="hbm2ddl.auto">create</property>
-->
<mapping resource="events/Event.hbm.xml" />
</session-factory>
</hibernate-configuration>
这里对于数据则采用了纯JAVA实现的HSQLDB。
import org.hibernate.Sess
|