編程環境:MyEclipse10 ,其中內建的 Hibernate 選用 Hibernate4.1
參考Java web王者歸來一書中的Hibernate例子,實現用Hibernate插入資料庫與查詢資料庫
編譯運行錯誤歸納:
1、AnnotationConfiguration()對象的使用
由於例子使用註解設定資料庫映射,所以必須用AnnotationConfiguration()對象,如果使用XML設定資料庫映射,則使用Configuration()對象;
但是在Hibernate3.6以後直接用Configuration()了,所以此處我們不需要修改
直接用:private static Configuration configuration = new Configuration();
2、Hibernate4.1版本中沒有buildSessionFactory()的下面用法
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
所以上條指令錯誤
我們可以修改為:
Configuration config = new Configuration().addResource("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
出現錯誤如下:
Initial SessinFactory creation failed.org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
Exception in thread "main" java.lang.ExceptionInInitializerError
at carrie.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:29)
at com.carrie.hiberanate.test.CatTest.main(CatTest.java:31)
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2277)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2273)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
at carrie.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:18)
錯誤原因:無法串連,然而我們設定檔hibernate.cfg.xml中已經配置好了dialect
修改:
Configuration config = new Configuration();
config.configure(); //必須要有此句話,且位置在configure和serviceRegistry之間
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
這樣才能找到設定檔hibernate.cfg.xml
3、Error applying BeanValidation relational constraints
錯誤:
資訊: Hibernate Validator bean-validator-3.0-JBoss-4.0.2
Initial SessinFactory creation failed.org.hibernate.HibernateException: Error applying BeanValidation relational constraints
Exception in thread "main" java.lang.ExceptionInInitializerError
at carrie.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:29)
at com.carrie.hiberanate.test.CatTest.main(CatTest.java:31)
Caused by: org.hibernate.HibernateException: Error applying BeanValidation relational constraints
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:219)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:126)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at carrie.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:24)
錯誤原因:
javax.persistence.validation.mode預設情況下是auto的,就是說如果不設定的話它是會自動去你的classpath下面找一個bean-validation**包,但是找不到,所以beanvalitionFactory錯誤
修改:
在hibernate.hbm.xml中加上
<property name="javax.persistence.validation.mode">none</property>
4、JDBC Driver class not found: com.mysql.jdbc.Driver
錯誤原因:驅動沒有載入
添加mysql-connector-java-5.1.25-bin.jar到工程
找到MYSQL安裝目錄,Connector J 5.1.25下的mysql-connector-java-5.1.25-bin.jar,將其添加到工程
本文出自 “carrie倩” 部落格,請務必保留此出處http://carrie1314.blog.51cto.com/6634326/1286445