编程环境: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.
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.
错误原因:无法连接,然而我们配置文件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.
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.
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at carrie.hibernate.util.HibernateUtil.
错误原因:
javax.persistence.validation.mode默认情况下是auto的,就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation**包,但是找不到,所以beanvalitionFactory错误
修改:
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,将其添加到工程