连接不能为null hibernate异常

时间:2012-05-22 11:36:08

标签: hibernate exception dialect

我是休眠的新手。当我尝试运行它时,我得到此异常“当hibernate.dialect未设置时,连接不能为空”。我已正确设置方言。

这是hibernate.cfg.xml文件

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>

<mapping class="org.siva.model.UserDetails"/>
</session-factory>
</hibernate-configuration>

模型类。

@Entity
public class UserDetails {
  @Id
  private int userID;
  private String userName;

  public int getUserID() {
    return userID;
  }
  public void setUserID(int userID) {
    this.userID = userID;
  }
  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
}

主要课程

public class HibernateTest {

public static void main(String[] args) {
   UserDetails user=new UserDetails();
   user.setUserID(1);
   user.setUserName("firstUser");
   Configuration configuration=new Configuration();
   ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
   SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
   Session session=sessionFactory.openSession();
   session.beginTransaction();
   session.save(user);
   session.getTransaction().commit();

}

}

1 个答案:

答案 0 :(得分:2)

您忘记配置配置。例如,使用此调用

Configuration configuration=new Configuration().configure();