我刚刚学,遇到问题每人问了,直接上代码,和问题吧
/hibernate4/src/hibernate.cfg.xml
<?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>        <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://10.97.144.117:1433;DatabaseName=MY_FIRST_DB</property>
        <property name="connection.username">JDBC</property>
        <property name="connection.password">123456</property>        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>        <!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> 注释掉原来的配置文件的映射方式-->
        
        <mapping package="cn.lcc.hibernaet.model" class="User" /><!-- 这儿我使用Annotatinon的方式进行数据库表的映射 -->    </session-factory></hibernate-configuration>/hibernate4/src/cn/lcc/hibernate/model/User.javapackage cn.lcc.hibernate.model;import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class User {
private int userId;
private String name;
private String password;
@Id
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}}
/hibernate4/src/cn/lcc/hibernate/test/TestUser.javapackage cn.lcc.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;import cn.lcc.hibernate.model.User;public class TestUser { /**
 * the model of hibernate 's test class 
 * @param args
 */
public static void main(String[] args) {
User user= new User();
user.setName("lcc");
user.setUserId(1);
user.setPassword("123456");

Configuration cfg = new Configuration();
org.hibernate.service.ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.configure().buildSessionFactory((org.hibernate.service.ServiceRegistry) serviceRegistry);
Session s = sf.openSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
s.close();
sf.close();
System.out.println("OK");
}}问题 异常如下:Exception in thread "main" 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 cn.lcc.hibernate.test.TestUser.main(TestUser.java:25)

解决方案 »

  1.   

    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
      

  2.   

    你没有设置hibernate dialect啊。在你的hibernate.cfg.xml加上
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> 应该就OK了!
     
      

  3.   

    sqlserver本身有个表名为user的表,可能是因为你的实体类建的表跟它重了,试试给你的user实体类一个表名称,@Table(name="User_tbl")。