hibernate4.x已經在官網出現一段時間了.
下載地址: http://hibernate.org/orm/downloads/
使用hibernate4所需要的jar包 在lib\required(必選的), 該文件夾中的jar必選都要copy,其他文件夾的可以有選擇性的copy;
1.buildSessionFactory
Configuration config = new Configuration();// SessionFactory factory = config.buildSessionFactory();
被下面退換,否則引入包會報錯 org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Configuration cfg = new Configuration().configure(); @SuppressWarnings("deprecation") ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);
2.annotation注解
org.hibernate.cfg.AnnotationConfiguration;
Deprecated. All functionality has been moved to Configuration
這個注解讀取配置的class已經廢棄,現在讀取配置不需要特別注明是注解,直接用Configuration cfg = new Configuration();就可以讀取注解。
Hibernate4.X版本中推薦使用annotation配置,所以在引進jar包時把requested里面的包全部引進來就已經包含了annotation必須包了
3.自動建表
Configuration cft = new Configuration().configure(); SchemaExport export = new SchemaExport(cft); export.create(true, true);
4.數據庫方言設置
<property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property> 在3.3版本中連接MySQL數據庫只需要指明MySQLDialect即可。在4.1版本中可以指出MySQL5Dialect