你好,游客 登录 注册 搜索
背景:
阅读新闻

Hibernate框架的搭建和第一个简单的实例

[日期:2012-02-29] 来源:Linux社区  作者:a352193394 [字体:大 中 小]

下面就可以运行这个了,记住,别忘了导入Mysql的Connection的Jar包。

  1. package com.bird.hibernate.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.   
  10. import com.bird.domain.User;  
  11.   
  12. public class Base {  
  13.   
  14.     /** 
  15.      * @param args 
  16.      */  
  17.     public static void main(String[] args) {  
  18.         Configuration cfg = new Configuration();  
  19.         cfg.configure();  
  20.           
  21.         @SuppressWarnings("deprecation")  
  22.         SessionFactory sf = cfg.buildSessionFactory();  
  23.           
  24.         Session s = sf.openSession();  
  25.           
  26.         Transaction tx = s.beginTransaction();  
  27.         User use = new User();  
  28.         use.setBirthday(new Date());  
  29.         use.setName("bird");  
  30.           
  31.         s.save(use);  
  32.         tx.commit();  
  33.         s.close();  
  34.           
  35.     }  
  36.   
  37. }  

运行结果如下

  1. 2012-2-28 12:12:38 org.hibernate.annotations.common.Version <clinit>  
  2. INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}  
  3. 2012-2-28 12:12:38 org.hibernate.Version logVersion  
  4. INFO: HHH000412: Hibernate Core {4.0.1.Final}  
  5. 2012-2-28 12:12:38 org.hibernate.cfg.Environment <clinit>  
  6. INFO: HHH000206: hibernate.properties not found  
  7. 2012-2-28 12:12:38 org.hibernate.cfg.Environment buildBytecodeProvider  
  8. INFO: HHH000021: Bytecode provider name : javassist  
  9. 2012-2-28 12:12:38 org.hibernate.cfg.Configuration configure  
  10. INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml  
  11. 2012-2-28 12:12:38 org.hibernate.cfg.Configuration getConfigurationInputStream  
  12. INFO: HHH000040: Configuration resource: /hibernate.cfg.xml  
  13. 2012-2-28 12:12:38 org.hibernate.cfg.Configuration addResource  
  14. INFO: HHH000221: Reading mappings from resource: com/bird/domain/User.hbm.xml  
  15. 2012-2-28 12:12:38 org.hibernate.cfg.Configuration doConfigure  
  16. INFO: HHH000041: Configured SessionFactory: null  
  17. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  18. INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)  
  19. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  20. INFO: HHH000115: Hibernate connection pool size: 20  
  21. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  22. INFO: HHH000006: Autocommit mode: false  
  23. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  24. INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///test]   
  25. 2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure  
  26. INFO: HHH000046: Connection properties: {user=root, password=****}  
  27. 2012-2-28 12:12:39 org.hibernate.dialect.Dialect <init>  
  28. INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect  
  29. 2012-2-28 12:12:39 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService  
  30. INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)  
  31. 2012-2-28 12:12:39 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>  
  32. INFO: HHH000397: Using ASTQueryTranslatorFactory  
  33. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute  
  34. INFO: HHH000228: Running hbm2ddl schema update  
  35. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute  
  36. INFO: HHH000102: Fetching database metadata  
  37. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute  
  38. INFO: HHH000396: Updating schema  
  39. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>  
  40. INFO: HHH000261: Table found: test.user  
  41. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>  
  42. INFO: HHH000037: Columns: [id, birthday, name]  
  43. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>  
  44. INFO: HHH000108: Foreign keys: []  
  45. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>  
  46. INFO: HHH000126: Indexes: [primary]  
  47. 2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute  
  48. INFO: HHH000232: Schema update complete  

这样就可以了

linux
相关资讯       hibernate 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款