Simple example of Hibernate

A simple example of Hibernate experience

12 * today looked to me that the easy Hibernate example of the results of an afternoon to get done. Little feelings and share with you the next. Later proceedings will be posted to the wrong place to show everyone.
1. Development Environment eclipse3.2.1, mysql5.0
2, the first development environment are configured on, I start most of the time are required for Hibernate put jar into window - refrences-java-installed jar and the results are compiled well too many times, but then I put all the deleted jar in projects to build up my Right-click and select Properties-java build path, select add external jars, the jar will be necessary to add
3, above, after the completion of the implementation of the procedures I found that the data should not be inserted into the database, but be able to show the sql insert statement and can be removed from the database data, then try again only to find the reasons for the time table is not the primary key ID is set to automatically increase , with the result that can not be inserted, the reasons are at my xml file to configure the primary key when using this <generator/>, and his corresponding to the database primary key must be automatically increased, this is only my understanding, because it can not find other reasons, the ^ _ ^.
Harvest on such points, give or take a look at the code now:
Jar file requirements:
antlr.jar
cglib.jar
asm.jar
asm-attrs.jar
common-collections.jar
common-logging.jar
ehcache.jar
hibernate3.jar
jta.jar
dom4j.jar
log4j.jar
mysql initialization statement:
# MySQL-Front 3.1 (Build 9.0)

# Host: localhost Database: demo
# ------------------------------------------------- -----
# Server version 5.0.22-community-nt

#
# Table structure for table user
#

CREATE TABLE `user` (
`id` int (6) NOT NULL auto_increment,
`name` varchar (255) NOT NULL,
`age` int (6) NOT NULL default'0 ',
PRIMARY KEY ( `id`)
) ENGINE = InnoDB DEFAULT CHARSET = gbk;

#
# Dumping data for table user
#

INSERT INTO `user` VALUES (1, 'david', 23);
INSERT INTO `user` VALUES (2, 'caterpillar', 30);
INSERT INTO `user` VALUES (3, 'caterpillar', 30);
# MySQL-Front 3.1 (Build 9.0)

# Host: localhost Database: demo
# ------------------------------------------------- -----
# Server version 5.0.22-community-nt

#
# Table structure for table user
#

CREATE TABLE `user` (
`id` int (6) NOT NULL auto_increment,
`name` varchar (255) NOT NULL,
`age` int (6) NOT NULL default'0 ',
PRIMARY KEY ( `id`)
) ENGINE = InnoDB DEFAULT CHARSET = gbk;

#
# Dumping data for table user
#

INSERT INTO `user` VALUES (1, 'david', 23);
INSERT INTO `user` VALUES (2, 'caterpillar', 30);
INSERT INTO `user` VALUES (3, 'caterpillar', 30);
# MySQL-Front 3.1 (Build 9.0)

# Host: localhost Database: demo
# ------------------------------------------------- -----
# Server version 5.0.22-community-nt

#
# Table structure for table user
#

CREATE TABLE `user` (
`id` int (6) NOT NULL auto_increment,
`name` varchar (255) NOT NULL,
`age` int (6) NOT NULL default'0 ',
PRIMARY KEY ( `id`)
) ENGINE = InnoDB DEFAULT CHARSET = gbk;

#
# Dumping data for table user
#

INSERT INTO `user` VALUES (1, 'david', 23);
INSERT INTO `user` VALUES (2, 'caterpillar', 30);
INSERT INTO `user` VALUES (3, 'caterpillar', 30);

# MySQL-Front 3.1 (Build 9.0)

# Host: localhost Database: demo
# ------------------------------------------------- -----
# Server version 5.0.22-community-nt

#
# Table structure for table user
#

CREATE TABLE `user` (
`id` int (6) NOT NULL auto_increment,
`name` varchar (255) NOT NULL,
`age` int (6) NOT NULL default'0 ',
PRIMARY KEY ( `id`)
) ENGINE = InnoDB DEFAULT CHARSET = gbk;

#
# Dumping data for table user
#

INSERT INTO `user` VALUES (1, 'david', 23);
INSERT INTO `user` VALUES (2, 'caterpillar', 30);
INSERT INTO `user` VALUES (3, 'caterpillar', 30);

Database configuration files: a user name and password set up according to their own database modifications
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="show_sql">
true
</ property>

<property name="dialect">
org.hibernate.dialect.MySQLDialect
</ property>

<property name="connection.driver_class">
com.mysql.jdbc.Driver
</ property>

<property name="connection.url">
jdbc: mysql: / / localhost: 3306/demo
</ property>

<property name="connection.username">
root
</ property>

<property name="connection.password">
851120
</ property>

<! - Object with the following settings database table mapping file ->
<mapping resource="onlyfun/caterpillar/User.hbm.xml"/>
<mapping resource="events/Event.hbm.xml"/>
</ session-factory>
</ hibernate-configuration>
user table corresponding categories:
package onlyfun.caterpillar;

public class User (
private Integer id;
private String name;
private Integer age;

public Integer getId () (
return id;
)

public void setId (Integer id) (
this.id = id;
)

public String getName () (
return name;
)

public void setName (String name) (
this.name = name;
)

public Integer getAge () (
return age;
)

public void setAge (Integer age) (
this.age = age;
)
)
user corresponding xml file
User.hbm.xml
<? xml version = "1.0" encoding = "utf-8"?>
<! DOCTYPE hibernate-mapping
PUBLIC "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name = "onlyfun.caterpillar.User"
table = "user">

<id name="id" column="id">
<generator/>
</ id>

<property name="name" column="name"/>

<property name="age" column="age"/>

</ class>

</ hibernate-mapping>
Control operation interface:
package onlyfun.caterpillar;

public interface IUserDAO (
public void insert (User user);
public User find (Integer id);
)
Interface implementation
package onlyfun.caterpillar;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class UserDAO implements IUserDAO (
private SessionFactory sessionFactory;

public UserDAO () (
)

public UserDAO (SessionFactory sessionFactory) (
this.setSessionFactory (sessionFactory);
)

public void setSessionFactory (SessionFactory sessionFactory) (
this.sessionFactory = sessionFactory;
)

public void insert (User user) (
/ / Get Session
Session session = sessionFactory.openSession ();
/ / Open the Services Transaction tx = session.beginTransaction ();
/ / Directly store objects session.save (user);
/ / Send Affairs tx.commit ();
session.close ();
)

public User find (Integer id) (
Session session = sessionFactory.openSession ();

User user = (User) session.get (User.class, id);

session.close ();

return user;
)
)
Test categories:
package onlyfun.caterpillar;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateDemo (
public static void main (String [] args) (
/ / Configuration responsible for the management of Hibernate configuration message Configuration config =
new Configuration (). configure ();
/ / Config set up under the SessionFactory
/ / SessionFactory will be used to set up Session
SessionFactory sessionFactory =
config.buildSessionFactory ();

/ / Set up DAO objects
IUserDAO userDAO = new UserDAO (sessionFactory);
User user = new User ();

user.setName ( "caterpillar");
user.setAge (30);

userDAO.insert (user);

user = userDAO.find (new Integer (1));

System.out.println ( "name:" + user.getName ()+"----"+ user.getAge ()+"----"+ user.getId ());
)
)
Stop

分类:Java 时间:2009-04-30 人气:1208
分享到:
blog comments powered by Disqus

相关文章

  • Configuration file to read xml file 2010-10-31

    1 to key-value of the stored information The information required in the code, can be key-valu write on the form of the configuration file. If put in the src directory HardDiskProperties.properties disk=c capacity=8G Test code public class BaseKeyVal

  • Ofbiz database table structure does not change the entry into force 2010-04-26

    ofbiz using the entity, the table structure are placed in entitymodel.xml file, but when the contents of the table structure was modified after the restart only to find the changes do not take effect. To make it effective, approach is simple: In Ofbi

  • Oracle table structure generated documentation (excel file) Tools 2010-06-28

    This tool is useful, beautiful table structure can generate the documentation (excel file format). Instructions for use: 1) use the Help file attachment in 2) the need to form the structure of comments, to generate a "field description" 3) A spe

  • Liferay 5.2.3 development environment to build 2010-05-02

    Liferay 5.2.3 development environment to build Prepared by: recky Version: 1.1 E-mail: 19968541@qq.com Date: 2010-03-04 1. About Liferay Liferay is a complete portal solution, based on J2EE applications, using technologies such as EJB and JMS, part o

  • SSH latest development environment and project examples of structures, recommend blood, told me to do step by step 2009-06-17

    See the online article has a lot of ssh configuration, but there is a lot of the tune does not make sense, there are versions, not exactly the same configuration, the following is my configuration to do the ssh tutorial development for reference; Thi

  • Developing Detail Explain Struts2 application - 1. Development environment set up 2010-09-15

    Description: This series is suitable for development experience with J2EE programmers quickly learn to use Struts2. 1, document preparation: From the following address http://struts.apache.org/download.cgi download files, you can choose * lib.zip or

  • Andriod development environment to build 2010-12-29

    Andriod development environment to build collections turnover several times, and finally build a good Andriod development environment, the following summarize the Andriod development environment to build the steps and precautions. Overall in three st

  • Easily create Silverlight development environment 2010-03-17

    Silverlight development environment to establish the following four steps: 1. First need to install the development tools, project development for Silverlight 3, you must use Visual Studio 2008 SP1 or Visual Web Developer Express with SP1 as a develo

  • MySQL export table structure (turn http://apps.hi.baidu.com/share/detail/15509617) 2010-09-14

    MySQL using mysqldump command to export the table structure Format mysqldump YourDatabaseName - user = YourUserName - password = YourPassword YourDatabaseName you want to deal with the database name YourUserName and YourPassword password correspondin

  • Several points of Silverlight development environment 2011-05-24

    Silverlight development environment, more stringent environmental requirements for the language, in the Chinese development environment, can not install the English version of the Silverlight SDK. Silverlight development environment to establish the

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.912 (s). 12 q(s)