Simple example of Hibernate

sponsored links

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
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Simple example of Hibernate

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Nan-Jing 5: When IBatis.Hibernate mixed affairs, pay attention to your SQL

    [Problem] Now, in the development of the so-called multi-storey JavaEE applications, data persistence layer is always essential, and "Automatic" of ORM - Hibernate, and "all-manual-type" of SqlMap - IBatis, equivalent data are Per ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate to use the principle of

    The use of hibernate, implementation of data persistence. Has the following several processes. One configuration database connection information. Hibernate.config 2 configuration mapping. 3 use: the use of the process are the following steps: 3.1: Ge ...

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Based on JDBC, JPA Annotation achieve simple CRUD Generic Dao

    The origin of ideas are pretty long history of reasons: [Use iBATIS history] The use of iBATIS has been a long time, the system is to use the CRUD template tool to generate the code, although there are tools to generate, but looked at a lot of CRUD the Sq

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries