org.hibernate.ejb.HibernatePersistence com.ncs.entity.UserInfo
注意:配置参数hibernate.hbm2ddl.auto 各个取值的含义
validate update create create-drop
这几个参数的作用主要用于:自动创建 更新 验证数据库表结构
如果不是此方面的需求取none
validate:加载hibernate时,验证创建数据库表结构
create:每次加载hibernate,重新创建数据库表结构,这就是导致数据库表结构丢失的原因
create-drop:加载hibernate时创建,退出是删除表结构
update:加载hibernate自动更新数据库结构
常见问题排错:
1.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named HelloTitan at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34) at Main.main(Main.java:10)
原因:没有Persistence provider
解决办法:错误提示很明显:No Persistence provider.
(1)检查lib:jar丢失,则将需要的lib添加到classpath中,Jboss使用的是hibernate实现,这些lib在下面的目录中能找到
jboss-X\common\lib\
(2)可能provider不对.检查xml文件里面的provider
2.
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: HelloTitan] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34) at Main.main(Main.java:10) Caused by: org.hibernate.HibernateException: Could not find datasource at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79) at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137) at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29) at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89) at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) ... 4 more Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) at javax.naming.InitialContext.lookup(InitialContext.java:392) at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75) ... 11 more
原因:JNDI没有初始化成功
解决办法: 在classpath添加 jnid.properties 文件 (以JBoss为例,配置如下)
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces java.naming.provider.url=127.0.0.1:1099
3.
程序成功运行,但是数据没有写入数据库
原因:没有提交数据
解决办法:em.getTransaction().commit();
附录
Hibernate SQL方言 (hibernate.dialect)
RDBMS | 方言 |
---|---|
DB2 | org.hibernate.dialect.DB2Dialect |
DB2 AS/400 | org.hibernate.dialect.DB2400Dialect |
DB2 OS390 | org.hibernate.dialect.DB2390Dialect |
PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
MySQL | org.hibernate.dialect.MySQLDialect |
MySQL with InnoDB | org.hibernate.dialect.MySQLInnoDBDialect |
MySQL with MyISAM | org.hibernate.dialect.MySQLMyISAMDialect |
Oracle (any version) | org.hibernate.dialect.OracleDialect |
Oracle 9i/10g | org.hibernate.dialect.Oracle9Dialect |
Sybase | org.hibernate.dialect.SybaseDialect |
Sybase Anywhere | org.hibernate.dialect.SybaseAnywhereDialect |
Microsoft SQL Server | org.hibernate.dialect.SQLServerDialect |
SAP DB | org.hibernate.dialect.SAPDBDialect |
Informix | org.hibernate.dialect.InformixDialect |
HypersonicSQL | org.hibernate.dialect.HSQLDialect |
Ingres | org.hibernate.dialect.IngresDialect |
Progress | org.hibernate.dialect.ProgressDialect |
Mckoi SQL | org.hibernate.dialect.MckoiDialect |
Interbase | org.hibernate.dialect.InterbaseDialect |
Pointbase | org.hibernate.dialect.PointbaseDialect |
FrontBase | org.hibernate.dialect.FrontbaseDialect |
Firebird | org.hibernate.dialect.FirebirdDialect |