首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

新手,hibernate配置有关问题

2012-12-14 
新手,hibernate配置问题本帖最后由 luoning1217 于 2012-11-13 14:50:29 编辑想用hibernate连sqlserver200

新手,hibernate配置问题
本帖最后由 luoning1217 于 2012-11-13 14:50:29 编辑 想用hibernate连sqlserver2005 express

用的最新版hibernate 4.1.8

主配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration >
<session-factory>
<property name = "hibernate.connection.url ">jdbc:sqlserver://localhost:1841;DatabaseName=Hibernate</property>
<property name = "hibernate.connection.driver_class ">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name = "hibernate.connection.username ">sa</property>
<property name = "hibernate.connection.password ">sa</property>
<property name = "hibernate.dialect ">org.hibernate.dialect.SQLServer2005Dialect</property>
<property name = "show_sql ">true</property>
<mapping resource="Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>


简单测试程序想保存一条记录:
package com.test.ognl;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.test.util.HibernateUtil;

public class HibernateTest1 {

public static void main(String[] args)
{
Session session = HibernateUtil.OpenSession();

Transaction tx = session.beginTransaction();

Customer customer = new Customer();

customer.setName("zhangsan");

try
{
session.save(customer);
tx.commit();
}
catch(Exception ex)
{
if(null != tx)
{
tx.rollback();
}
}
finally
{
session.close();
}
}
}

运行后报如下错误:
2012/11/13 14:45:56 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
2012/11/13 14:45:56 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.8.Final}
2012/11/13 14:45:56 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2012/11/13 14:45:56 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2012/11/13 14:45:56 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2012/11/13 14:45:56 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2012/11/13 14:45:57 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Customer.hbm.xml
2012/11/13 14:45:57 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
2012/11/13 14:45:57 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null


2012/11/13 14:45:57 org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
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:77)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748)
at com.test.util.HibernateUtil.<clinit>(HibernateUtil.java:20)
at com.test.ognl.HibernateTest1.main(HibernateTest1.java:12)
Exception in thread "main" java.lang.NullPointerException
at com.test.util.HibernateUtil.OpenSession(HibernateUtil.java:31)
at com.test.ognl.HibernateTest1.main(HibernateTest1.java:12)

貌似连接不成功,请指教
[最优解释]
不对楼主,楼上的回答有点不太对。sql2005是像你这么配置的。
你的配置是对的,报这个错可能是没有读取到配置文件。
import com.test.util.HibernateUtil;这个HibernateUtil是楼主自己写的吗?hibernate4和3读取配置文件是不同的,建议楼主自己写。或者看下面的简单的读取配置文件的代码。hibernate.cfg.xml文件位于src跟目录下面。


 Configuration config = new Configuration();
 config.configure();
 serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
 sessionFactory = config.buildSessionFactory(serviceRegistry);
 Session session = sessionFactory.getCurrentSession();


如果上面的代买还是报错,在配置文件中增加一行配置:

<property name="javax.persistence.validation.mode">none</property>


如果上面的代码还是报错,那
[其他解释]
楼主你的配置出了问题,这个链接的写法有点怪异

<property name = "hibernate.connection.url ">jdbc:sqlserver://localhost:1841;DatabaseName=Hibernate</property>


改成下面这个试试:yourDatabaseName就是你链接数据的名字,你数据库名称是Hibernate?
还有用户名密码是sa吗?确认一下吧

<property name = "hibernate.connection.url ">jdbc:sqlserver://localhost:1841:yourDatabaseName</property>

热点排行