Hibernate4 Annotation实例

来源:互联网 发布:软件学报 期刊ccf 编辑:程序博客网 时间:2024/06/12 00:36

hibernate.cfg.xml 配置~

[html] view plaincopyprint?
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  3. <hibernate-configuration>  
  4.     <session-factory>  
  5.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  6.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>  
  7.         <property name="hibernate.connection.username">root</property>  
  8.         <property name="hibernate.connection.password">123456</property>  
  9.         <property name="hibernate.connection.pool_size">10</property>  
  10.         <property name="show_sql">true</property>  
  11.         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>  
  12.         <property name="hibernate.current_session_context_class">thread</property>  
  13.         <property name="hbm2ddl.auto">create</property>  
  14.     </session-factory>  
  15. </hibernate-configuration>  


实体类

[java] view plaincopyprint?
  1. package com.neal.entity;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.GeneratedValue;  
  9. import javax.persistence.GenerationType;  
  10. import javax.persistence.Id;  
  11. import javax.persistence.Temporal;  
  12. import javax.persistence.TemporalType;  
  13.   
  14. @Entity  
  15. public class User implements Serializable{  
  16.     /** 
  17.      *  
  18.      */  
  19.     private static final long serialVersionUID = 1L;  
  20.     private int id;  
  21.     private String name;  
  22.     private Date birthday;  
  23.       
  24.     @Id @GeneratedValue(strategy=GenerationType.AUTO)  
  25.     public int getId() {  
  26.         return id;  
  27.     }  
  28.     public void setId(int id) {  
  29.         this.id = id;  
  30.     }  
  31.     @Column(length=10)  
  32.     public String getName() {  
  33.         return name;  
  34.     }  
  35.     public void setName(String name) {  
  36.         this.name = name;  
  37.     }  
  38.     @Temporal(TemporalType.DATE)  
  39.     public Date getBirthday() {  
  40.         return birthday;  
  41.     }  
  42.     public void setBirthday(Date birthday) {  
  43.         this.birthday = birthday;  
  44.     }  
  45.       
  46.       
  47.       
  48. }  


 

最后的测试类

[java] view plaincopyprint?
  1. package junit.test;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.Transaction;  
  8. import org.hibernate.cfg.Configuration;  
  9. import org.hibernate.service.ServiceRegistry;  
  10. import org.hibernate.service.ServiceRegistryBuilder;  
  11. import org.junit.BeforeClass;  
  12. import org.junit.Test;  
  13.   
  14. import com.neal.entity.User;  
  15.   
  16. public class HibernateTest {  
  17.   
  18.     private static SessionFactory sessionFactory;  
  19.     @BeforeClass  
  20.     public static void setUpBeforeClass() throws Exception {  
  21.         Configuration cfg = new Configuration();      
  22.         cfg.configure(); // 重要  
  23.         ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();  
  24.         try {  
  25.             sessionFactory = cfg.configure()  
  26.                     .addAnnotatedClass(User.class)  
  27.                     .buildSessionFactory(sr);  
  28.         } catch (Exception e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34.     @Test  
  35.     public void test() {  
  36.           
  37.         Session session = sessionFactory.openSession();  
  38.         Transaction tx = session.beginTransaction();  
  39.         User user = new User();  
  40.         user.setBirthday(new Date());  
  41.         user.setName("hello");  
  42.           
  43.         session.persist(user);  
  44.         tx.commit();  
  45.         session.close();  
  46.         System.out.println("end");  
  47.     }  
  48.   
  49. }  


 

结果输出~

[plain] view plaincopyprint?
  1. 2012-3-21 19:50:27 org.hibernate.annotations.common.Version <clinit>  
  2. INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}  
  3. 2012-3-21 19:50:27 org.hibernate.Version logVersion  
  4. INFO: HHH000412: Hibernate Core {4.1.1}  
  5. 2012-3-21 19:50:27 org.hibernate.cfg.Environment <clinit>  
  6. INFO: HHH000206: hibernate.properties not found  
  7. 2012-3-21 19:50:27 org.hibernate.cfg.Environment buildBytecodeProvider  
  8. INFO: HHH000021: Bytecode provider name : javassist  
  9. 2012-3-21 19:50:27 org.hibernate.cfg.Configuration configure  
  10. INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml  
  11. 2012-3-21 19:50:27 org.hibernate.cfg.Configuration getConfigurationInputStream  
  12. INFO: HHH000040: Configuration resource: /hibernate.cfg.xml  
  13. 2012-3-21 19:50:27 org.hibernate.cfg.Configuration doConfigure  
  14. INFO: HHH000041: Configured SessionFactory: null  
  15. 2012-3-21 19:50:27 org.hibernate.cfg.Configuration configure  
  16. INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml  
  17. 2012-3-21 19:50:27 org.hibernate.cfg.Configuration getConfigurationInputStream  
  18. INFO: HHH000040: Configuration resource: /hibernate.cfg.xml  
  19. 2012-3-21 19:50:27 org.hibernate.cfg.Configuration doConfigure  
  20. INFO: HHH000041: Configured SessionFactory: null  
  21. 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  22. INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)  
  23. 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  24. INFO: HHH000115: Hibernate connection pool size: 10  
  25. 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  26. INFO: HHH000006: Autocommit mode: false  
  27. 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  28. INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]  
  29. 2012-3-21 19:50:27 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  30. INFO: HHH000046: Connection properties: {user=root, password=****}  
  31. 2012-3-21 19:50:28 org.hibernate.dialect.Dialect <init>  
  32. INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect  
  33. 2012-3-21 19:50:28 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation  
  34. INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4  
  35. 2012-3-21 19:50:28 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService  
  36. INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)  
  37. 2012-3-21 19:50:28 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>  
  38. INFO: HHH000397: Using ASTQueryTranslatorFactory  
  39. 2012-3-21 19:50:28 org.hibernate.tool.hbm2ddl.SchemaExport execute  
  40. INFO: HHH000227: Running hbm2ddl schema export  
  41. Hibernate: drop table if exists User  
  42. Hibernate: create table User (id integer not null auto_increment, birthday date, name varchar(10), primary key (id))  
  43. 2012-3-21 19:50:28 org.hibernate.tool.hbm2ddl.SchemaExport execute  
  44. INFO: HHH000230: Schema export complete  
  45. Hibernate: insert into User (birthday, name) values (?, ?)  
  46. end  
原创粉丝点击