Some days ago. I bought a book named Java Persistence with Hibernate.
Now , I will show a simple sample for everybody who wanna learn the skill of Hibernate.
Step one.
Create a class named Message,Of course ,you should have created a project in IDE.
Message.java
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 Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Step two.
create a XML named Message.hbm.xml.Just like the following.
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 ="MESSAGES">
<id name="id" column="MESSAGE_ID">
<generator class="increment"/>
</id>
<property name="text" column="MESSAGE_TEXT"/>
<many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID" foreign-key="FK_NEXT_MESSAGE"/>
</class>
</hibernate-mapping>
Step three.
Write you property file.
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>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@126.1.1.63:1521:oradb</property>
<property name="hibernate.connection.username">timesheet</property>
<property name="hibernate.connection.password">timesheet</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="hello/Message.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Step four
Configurating you DataBase,and run the main method like this
Oh.I forgot that ,before you run this project ,you'd better create a manage bena for Hibernate.
I write a class named HibernateUtil.
HibernateUtil.java
package persistence;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Throwable ex)
{
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
public static void shutdown()
{
getSessionFactory().close();
}
}
then you should have the Main Class lik this.
package hello;
import org.hibernate.*;
import java.util.*;
import persistence.*;
/**
*
* @author zhangqi
*/
public class HelloWorld {
public static void main(String[] args)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello World2");
Long msgId =(Long)session.save(message);
System.out.println(msgId);
tx.commit();;
session.close();
Session newSession = HibernateUtil.getSessionFactory().openSession();
Transaction newTx = newSession.beginTransaction();
List messages = newSession.createQuery("from Message m order by m.text asc").list();
System.out.println(messages.size()+" message(s) found:");
for(Iterator iter = messages.iterator();iter.hasNext();)
{
Message loadedMsg =(Message)iter.next();
System.out.println(loadedMsg.getId() +" "+loadedMsg.getText());
}
newTx.commit();;
newSession.close();
HibernateUtil.shutdown();
}
}
Yeah.run it ,you will get the answer
分享到:
相关推荐
simple_php
Walk-through of Walk a Simple Hibernate Example Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/Course-Materials/hibernate.html
一个 ORACLE AQ 初始化 和测试的 样例程序。
a sample program of Simple Protocol
A Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLAB
Dropout:A Simple Way to Prevent Neural Networks from Overfitting.zip
A simple echo Server
WATCH THE UNOBSERVED_ A SIMPLE APPROACH TO PARALLELIZING
Arcgis server 9.2 的simple arcgis server view sample学习笔记
jcaptcha-integration-simple-servlet-sample.jar 验证码, jcaptcha组件,验证码项目
A simple calculator program.
Hibernate: A Developer's Notebook shows you how to use Hibernate to automate persistence: you write natural Java objects and some simple configuration files, and Hibernate automates all the ...
一阶差分麦克风 差分麦克风 自适应 差分麦克风 自适应 差分麦克风 自适应 差分麦克风 自适应
We want to offer a short and simple MATLAB code, described in more detail than usual, so the reader can experiment (and add to the code) knowing the underlying principles. We find the node locations ...
A simple airline ticket reservation program 英文题目,里面有,代码是上课我自己交的
simple-sharding, ☕️ A simple database shard middleware.zip
A simple neural attentive meta-learner
codeproject 《A Simple IOCP Server/Client Class》 整理修改版
A Simple COM tutorial using ATL使用ATL简单COM指南
a one simple 2.49版 皮肤检测仪驱动程序/安装程序 a one simple 有两个软件版本,2.43和2.49版,一般情况下使用2.49版即可,个别购买年代久远的机子使用2.49版,如有任何疑问,请联系软件中的客服人员,谢谢 感谢...