Hibernate4 Annotation实例简单介绍.
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>
- <property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
- <property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/test</property>
- <property name=”hibernate.connection.username”>root</property>
- <property name=”hibernate.connection.password”>123456</property>
- <property name=”hibernate.connection.pool_size”>10</property>
- <property name=”show_sql”>true</property>
- <property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property>
- <property name=”hibernate.current_session_context_class”>thread</property>
- <property name=”hbm2ddl.auto”>create</property>
- </session-factory>
- </hibernate-configuration>
<?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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hbm2ddl.auto">create</property> </session-factory> </hibernate-configuration>
实体类
- package com.neal.entity;
- import java.io.Serializable;
- import java.util.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Temporal;
- import javax.persistence.TemporalType;
- @Entity
- public class User implements Serializable{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private int id;
- private String name;
- private Date birthday;
- @Id @GeneratedValue(strategy=GenerationType.AUTO)
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- @Column(length=10)
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Temporal(TemporalType.DATE)
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- }
package com.neal.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity public class User implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private Date birthday; @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(length=10) public String getName() { return name; } public void setName(String name) { this.name = name; } @Temporal(TemporalType.DATE) public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
最后的测试类
- package junit.test;
- import java.util.Date;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import com.neal.entity.User;
- public class HibernateTest {
- private static SessionFactory sessionFactory;
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- Configuration cfg = new Configuration();
- cfg.configure(); // 重要
- ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
- try {
- sessionFactory = cfg.configure()
- .addAnnotatedClass(User.class)
- .buildSessionFactory(sr);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Test
- public void test() {
- Session session = sessionFactory.openSession();
- Transaction tx = session.beginTransaction();
- User user = new User();
- user.setBirthday(new Date());
- user.setName(“hello”);
- session.persist(user);
- tx.commit();
- session.close();
- System.out.println(“end”);
- }
- }
package junit.test; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.BeforeClass; import org.junit.Test; import com.neal.entity.User; public class HibernateTest { private static SessionFactory sessionFactory; @BeforeClass public static void setUpBeforeClass() throws Exception { Configuration cfg = new Configuration(); cfg.configure(); // 重要 ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); try { sessionFactory = cfg.configure() .addAnnotatedClass(User.class) .buildSessionFactory(sr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void test() { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setBirthday(new Date()); user.setName("hello"); session.persist(user); tx.commit(); session.close(); System.out.println("end"); } }
结果输出~
- 2012-3-21 19:50:27 org.hibernate.annotations.common.Version <clinit>
- INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
- 2012-3-21 19:50:27 org.hibernate.Version logVersion
- INFO: HHH000412: Hibernate Core {4.1.1}
- 2012-3-21 19:50:27 org.hibernate.cfg.Environment <clinit>
- INFO: HHH000206: hibernate.properties not found
- 2012-3-21 19:50:27 org.hibernate.cfg.Environment buildBytecodeProvider
- INFO: HHH000021: Bytecode provider name : javassist
- 2012-3-21 19:50:27 org.hibernate.cfg.Configuration configure
- INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
- 2012-3-21 19:50:27 org.hibernate.cfg.Configuration getConfigurationInputStream
- INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
- 2012-3-21 19:50:27 org.hibernate.cfg.Configuration doConfigure
- INFO: HHH000041: Configured SessionFactory: null
- 2012-3-21 19:50:27 org.hibernate.cfg.Configuration configure
- INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
- 2012-3-21 19:50:27 org.hibernate.cfg.Configuration getConfigurationInputStream
- INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
- 2012-3-21 19:50:27 org.hibernate.cfg.Configuration doConfigure
- INFO: HHH000041: Configured SessionFactory: null
- 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
- INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
- 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
- INFO: HHH000115: Hibernate connection pool size: 10
- 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
- INFO: HHH000006: Autocommit mode: false
- 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
- INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]
- 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
- INFO: HHH000046: Connection properties: {user=root, password=****}
- 2012-3-21 19:50:28 org.hibernate.dialect.Dialect <init>
- INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
- 2012-3-21 19:50:28 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
- INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
- 2012-3-21 19:50:28 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
- INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
- 2012-3-21 19:50:28 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
- INFO: HHH000397: Using ASTQueryTranslatorFactory
- 2012-3-21 19:50:28 org.hibernate.tool.hbm2ddl.SchemaExport execute
- INFO: HHH000227: Running hbm2ddl schema export
- Hibernate: drop table if exists User
- Hibernate: create table User (id integer not null auto_increment, birthday date, name varchar(10), primary key (id))
- 2012-3-21 19:50:28 org.hibernate.tool.hbm2ddl.SchemaExport execute
- INFO: HHH000230: Schema export complete
- Hibernate: insert into User (birthday, name) values (?, ?)
- end
2012-3-21 19:50:27 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 2012-3-21 19:50:27 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.1.1} 2012-3-21 19:50:27 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 2012-3-21 19:50:27 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 2012-3-21 19:50:27 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 2012-3-21 19:50:27 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 2012-3-21 19:50:27 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 2012-3-21 19:50:27 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 2012-3-21 19:50:27 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 2012-3-21 19:50:27 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 10 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: false 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test] 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****} 2012-3-21 19:50:28 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 2012-3-21 19:50:28 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 2012-3-21 19:50:28 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 2012-3-21 19:50:28 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory 2012-3-21 19:50:28 org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000227: Running hbm2ddl schema export Hibernate: drop table if exists User Hibernate: create table User (id integer not null auto_increment, birthday date, name varchar(10), primary key (id)) 2012-3-21 19:50:28 org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000230: Schema export complete Hibernate: insert into User (birthday, name) values (?, ?) end
THE END~~