利用hibernate in action来学习hibernate,接触其第一个例子,运行成功。其过程缩写如下,以备查阅。
1.建立项目目录,引入包:
.src
.lib
2.建立数据库表message(id,text,next_message_id);
3.编写Mssage.java:这个类和数据库当中的message表相对应:
package hello;
public class Message{
private Long id;
private String text;
private Message nextMessage;
Message(){
}
public Message(String text){
this.text = text;
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getText(){
return text;
}
public void setText(String text){
this.text = text;
}
public Message getNextMessage(){
return nextMessage;
}
public void setNextMessage(Message nextMessage){
this.nextMessage = nextMessage;
}
}
4.编写其映射文件message.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="hello.Message" table="MESSAGE"> <id name="id" column="ID"> <generator class="increment" /> </id> <property name="text" column="TEXT" /> <many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID" foreign-key="FK_NEXT_MESSAGE" /> </class> </hibernate-mapping>
5.编写HibernateUtil.java来对SessionFactory进行管理
package persistence;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil{
private static SessionFactory sessionFactory;
static{
try{
sessionFactory = new Configuration()
.configure().buildSessionFactory();
}catch(Exception ex){
ex.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static void shutdown(){
getSessionFactory().close();
}
}
6.编写hibernate.cfg.xml来对hibernate进行配置:
<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max)size">20</property> <property name="hibernate.c3p0.timeout">300</property> <property name="hibernate.c3p0.max)statements">50</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="myeclipse.connection.profile">mysql</property> <property name="connection.url"> jdbc:mysql://localhost:3306/mydb </property> <property name="connection.username">root</property> <property name="connection.password">1</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <mapping resource="hello/Message.hbm.xml" /> </session-factory> </hibernate-configuration>
7.编写HelloWorld.java运行主类:
package hello;
import java.util.*;
import org.hibernate.*;
import persistence.*;
public class HelloWorld{
public static void main(String[] args){
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message m = new Message("Hello World");
session.save(m);
tx.commit();
session.close();
HibernateUtil.shutdown();
}
}
8.编写ant运行脚本:
<project name="HelloWorld" default="compile" basedir="."> <property name="proj.name" value="HelloWorld"/> <property name="proj.version" value="1.0"/> <property name="src.java.dir" value="src"/> <property name="lib.dir" value="lib"/> <property name="build.dir" value="bin"/> <path id="project.classpath"> <fileset dir="lib"> <include name="**/*.jar"/> <include name="*.jar"/> </fileset> </path> <patternset id="meta.files"> <include name="**/*.xml"/> <include name="**/*.properties"/> </patternset> <target name="clean"> <delete dir="${build.dir}"/> <mkdir dir="${build.dir}"/> </target> <target name="compile" depends="clean"> <mkdir dir="${build.dir}"/> <javac srcdir="${src.java.dir}" destdir="${build.dir}" classpathref="project.classpath"> </javac> </target> <target name="copymetafiles"> <copy todir="${build.dir}"> <fileset dir="${src.java.dir}"> <patternset refid="meta.files"/> </fileset> </copy> </target> <target name="run" depends="compile,copymetafiles" description="Build and run HelloWorld"> <java fork="true" classname="hello.HelloWorld" classpathref="project.classpath"> <classpath path="${build.dir}"/> </java> </target> </project>
最后运行ant run来编译、运行项目。
最后的项目目录结构如下:
helloworld
/src
/hello
HelloWord.java
Message.java
message.hbm.xml
/persistence
HibernateUtil.java
hibernate.cfg.xml
/lib
build.xml