第一個Hibernate程式——錯誤整理

來源:互聯網
上載者:User


編程環境: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.