很久没写blog , 很久没用hibernate 了. 有点生疏了.
Hibernate 3.6.10 的目录结构比之前 3.2 改了许多,让我都有点不习惯了.
|- documentation 文档资料
|- lib 支持类
|- jpa 强烈建议使用
|- optional 可选择 lib , 有连接池和缓存.
|- required 必须的支持 lib.
|- project
|- etc config file .
|- source code.
|- hibernate3.jar
包结构弄清晰了,开始下一步. 做些简单的回忆例子.
hibernate 的例子分两类,1. hbm配置, 2. 标注.
------------- begin hbm config.
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">org.h2.Driver</property> <!-- save in memory <property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property> --> <!-- save in hard disk --> <property name="connection.url">jdbc:h2:~/test</property> <property name="connection.username">sa</property> <property name="connection.password"/> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.H2Dialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/demo/Event.hbm.xml"/> </session-factory> </hibernate-configuration>
Event.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.demo"> <class name="Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="increment"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"/> </class> </hibernate-mapping>Event.java
package com.demo; import java.util.Date; public class Event { private Long id; private String title; private Date date; public Event() { // this form used by Hibernate } public Event(String title, Date date) { // for application use, to create new events this.title = title; this.date = date; } public Long getId() { return id; } private void setId(Long id) { this.id = id; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
Create a test class , for test the CURD operation
package com.demo; import java.util.Date; import java.util.List; import junit.framework.TestCase; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * Illustrates use of Hibernate native APIs. * * @author Steve Ebersole */ public class NativeApiIllustrationTest extends TestCase { private SessionFactory sessionFactory; @Override protected void setUp() throws Exception { // A SessionFactory is set up once for an application sessionFactory = new Configuration() .configure() // configures settings from hibernate.cfg.xml .buildSessionFactory(); } @Override protected void tearDown() throws Exception { if ( sessionFactory != null ) { sessionFactory.close(); } } public void testBasicUsage() { // create a couple of events... Session session = sessionFactory.openSession(); session.beginTransaction(); session.save( new Event( "Our very first event!", new Date() ) ); session.save( new Event( "A follow up event", new Date() ) ); session.getTransaction().commit(); session.close(); // now lets pull events from the database and list them session = sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery( "from Event" ).list(); for ( Event event : (List<Event>) result ) { System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() ); } session.getTransaction().commit(); session.close(); } }Do not for get download H2 DB, H2 DB is for test .
----------- begin annotations
add to hibernate.cfg.xml
<mapping class="com.demo.Person" />Person.java
package com.demo; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name = "Person") public class Person { private String id; private String name; private int age; private Date birthday; public Person() { super(); } public Person(String id, String name, int age, Date birthday) { super(); this.id = id; this.name = name; this.age = age; this.birthday = birthday; } @Id public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="age") public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Column(name="birthday") @Temporal(TemporalType.DATE) public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }PersonTest.java
package com.demo; import java.util.Date; import java.util.List; import junit.framework.TestCase; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * Illustrates use of Hibernate native APIs. * * @author Steve Ebersole */ public class PersonTest extends TestCase { private SessionFactory sessionFactory; @Override protected void setUp() throws Exception { // A SessionFactory is set up once for an application sessionFactory = new Configuration() .configure() // configures settings from hibernate.cfg.xml .buildSessionFactory(); } @Override protected void tearDown() throws Exception { if ( sessionFactory != null ) { sessionFactory.close(); } } public void testBasicUsage() { // create a couple of events... Session session = sessionFactory.openSession(); session.beginTransaction(); session.save( new Person(String.valueOf(System.nanoTime()),"John",26,new Date()) ); session.save( new Person(String.valueOf(System.nanoTime()),"Mike",30,new Date()) ); session.getTransaction().commit(); session.close(); // now lets pull events from the database and list them session = sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery( "from Person" ).list(); for ( Person person : (List<Person>) result ) { System.out.println( "Person (" + person.getName() + ") : " + person.getAge() ); } session.getTransaction().commit(); session.close(); } }
It seems done . If any checked exception , please join in the JUnit 4 or check the problem in detail.