`
shenshuibomb
  • 浏览: 25298 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

A simple Hibernate sample

阅读更多

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

分享到:
评论

相关推荐

    ORACLE SIMPLE AQ SAMPLE

    在"ORACLE SIMPLE AQ SAMPLE"中,我们可以通过提供的样例程序了解如何初始化和测试 Oracle AQ 的基本操作。首先,让我们详细探讨一下 Oracle AQ 的核心概念和功能: 1. **消息队列**:Oracle AQ 中的消息队列是存储...

    A Simple Mesh Generator in MATLAB

    A Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLAB

    some simple demo about hibernate3.2 and spring2.5

    标题 "some simple demo about hibernate3.2 and spring2.5" 暗示这是一个关于如何集成和使用 Hibernate 3.2 和 Spring 2.5 的简单演示项目。这两个库在Java开发中扮演着重要角色,Hibernate是流行的对象关系映射...

    DirectXTK Simple Win32 Sample

    这个"DirectXTK Simple Win32 Sample"是一个示例项目,旨在演示如何使用DirectXTK在Win32应用程序中加载和渲染3D模型文件。下面我们将详细讨论这个样本中的关键知识点。 首先,DirectX 11是微软提供的低级图形API,...

    jcaptcha-integration-simple-servlet-sample.jar

    jcaptcha-integration-simple-servlet-sample.jar 验证码, jcaptcha组件,验证码项目

    a sample program of Simple Protocol

    本篇将详细解析标题为“a sample program of Simple Protocol”的示例程序,帮助读者深入理解简易协议的工作原理及其应用。 首先,我们要明确简易协议的核心概念:它通常基于文本,易于解析,适合小型系统间的通信...

    a_simple_windows_sample.rar_simple windows

    这个“a_simple_windows_sample.rar”压缩包文件提供的就是一个简单的Windows应用程序示例,对于初学者来说是很好的学习资源。下面将详细解释这个示例中的关键知识点。 1. **Visual C++**: Visual C++是微软开发的...

    Hibernate_A Developers Notebook

    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 ...

    A SIMPLE MESH GENERATOR IN MATLAB

    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 ...

    hibernate必要的jar包

    5. **slf4j-api.jar**和相应的实现(如logback-classic.jar或log4j.jar):SLF4J(Simple Logging Facade for Java)是一个日志记录抽象层,它允许你选择不同的日志框架作为后端。在Hibernate中,你需要一个具体的...

    simple arcgis server view sample学习笔记

    在ArcGIS Server 9.2中,Simple ArcGIS Server View Sample是一个基础的示例,用于帮助开发者理解如何在Web环境中部署和使用ArcGIS服务。这个学习笔记主要关注了ArcGIS Server与Web应用集成的关键组件和配置过程。 ...

    hibernate需要的10个jar

    9. **slf4j-simple.jar**:这是SLF4J的一个简单实现,提供基本的日志输出。在开发环境中,你可以使用此JAR进行日志记录,但在生产环境中,可能会选择更复杂、更全面的日志系统。 10. **mysql-connector-java.jar**...

    a one simple 2.49版 皮肤检测仪驱动程序/安装程序

    a one simple 2.49版 皮肤检测仪驱动程序/安装程序 a one simple 有两个软件版本,2.43和2.49版,一般情况下使用2.49版即可,个别购买年代久远的机子使用2.49版,如有任何疑问,请联系软件中的客服人员,谢谢 感谢...

    a one simple 2.43版 皮肤检测仪驱动程序/安装程序

    a one simple 2.43版 皮肤检测仪驱动程序/安装程序 a one simple 有两个软件版本,2.43和2.49版,一般情况下使用2.49版即可,个别购买年代久远的机子使用2.49版,如有任何疑问,请联系软件中的客服人员,谢谢 感谢...

    A SIMPLE MODEL OF THE BELOUSOV-ZHABOTINSKY REACTION FROM FIRST PRINCIPLES

    automata models of BZ spirals can be created by using a very simple set of equations based on a three substrate model with close connection to reaction-diffusion models, more closely resembling the ...

    SimpleSample.rar

    基于DirectX的C++程序

    Hibernate lib

    9. **其他依赖库**:如SLF4J(Simple Logging Facade for Java)用于日志记录,或DOM4J、Xerces等XML处理库,这些是Hibernate运行所必需的。 了解了这些组件后,开发者可以快速搭建Hibernate环境,进行数据库操作。...

    simple-sharding, A simple database shard middleware.zip

    《简单分库中间件simple-sharding的探索与实践》 在大数据时代,数据库的扩展性和性能成为了企业系统架构的关键挑战之一。"simple-sharding",正如其名,是一款旨在简化数据库分片操作的中间件,它作为一个轻量级的...

    《A Simple IOCP Server/Client Class》

    本文将基于《A Simple IOCP Server/Client Class》这一项目,深入探讨IOCP的基本概念、工作原理以及如何在实际编程中应用。** **一、IOCP基础** 1. **什么是IOCP** IOCP,全称Input/Output Completion Port,是一...

Global site tag (gtag.js) - Google Analytics