hibernate的常見錯誤

hibernate的常見錯誤

出現 org.hibernate.HibernateException:No CurrentSessionContext configured! 這個錯誤
<!-- more -->
一般這個報錯都是hibernate.cfg.xml的配置出現了問題

> 例如:<property name="hibernate_current_session_context_class">thread</property>

> 看似沒有錯誤

> 正確的寫法<property name="hibernate.current_session_context_class">thread</property>

> 有些人非常容易忽視這個錯誤,比如我!!

這個hibernate 其他的核心寫法

Users映射代碼

 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <!-- Generated 2016-11-26 15:34:57 by Hibernate Tools 3.4.0.CR1 -->
 <hibernate-mapping>
     <class name="entity.Users" table="USERS">
    
  <id name="uid" type="int">
      <generator class="native" />
  </id>
  
  <property name="username" type="java.lang.String" />
  <property name="password" type="java.lang.String" />
     </class>
    
 </hibernate-mapping>

* 實體類代碼

 package entity;

 public class Users {
      
   private int uid;
   private String username;
   private String password;
  
  public Users(int uid, String username, String password) {
   super();
   this.uid = uid;
   this.username = username;
   this.password = password;
  }
  public Users() {
   super();
   // TODO Auto-generated constructor stub
  }
  public int getUid() {
   return uid;
  }
  public void setUid(int uid) {
   this.uid = uid;
  }
  public String getUsername() {
   return username;
  }
  public void setUsername(String username) {
   this.username = username;
  }
  public String getPassword() {
   return password;
  }
  public void setPassword(String password) {
   this.password = password;
  }
  
 }
    

* hibernate.cfg.xml的配置

 <!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.driver_class">com.mysql.jdbc.Driver</property>
   <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
   <property name="hibernate.connection.username">root</property>
   <property name="hibernate.connection.password">123456</property>
   <!-- hibernate配置數據庫方言 -->
   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   <!-- 2. 其他相關配置 -->
   <!-- 2.1 顯示hibernate在運行時候執行的sql語句 -->
   <property name="show_sql">true</property>
   <!-- 2.2 格式化sql -->
   <property name="format_sql">true</property>
   <!-- 2.3 自動建表 -->
   <property name="hbm2ddl.auto">update</property>
   <property name="hibernate.current_session_context_class">thread</property>

   <mapping resource="entity/Students.hbm.xml" />
   <mapping resource="entity/Users.hbm.xml" />

  </session-factory>
 </hibernate-configuration>


* 單例模式設置

 package db;

 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.service.ServiceRegistry;
 import org.hibernate.service.ServiceRegistryBuilder;


 public class MyHibernateSessionFaction {

  private  static SessionFactory sessionFactory; //會話工廠屬性
  
  //構造方法私有化。保證單例模式
  private MyHibernateSessionFaction(){
   
  }
  
  //公有的靜態方法,獲得會話工廠對象
  public static SessionFactory getSessionFactory(){
   if(sessionFactory==null){
    Configuration config=new Configuration().configure();
    
    ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).build();
    sessionFactory=config.buildSessionFactory(serviceRegistry);
    return sessionFactory;
   }else{
    return sessionFactory;
   }
  }
  
 }

* 登錄的實現方法

 package service.impl;


 import java.util.List;


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

 import service.UsersDAO;
 import db.MyHibernateSessionFaction;
 import entity.Users;

 public class UsersDAOImpl implements UsersDAO{

  @Override
  public boolean usersLogiin(Users u) {
   // TODO Auto-generated method stub
   Transaction tx=null;
   String hql="";
   try{
    Session session=MyHibernateSessionFaction.getSessionFactory().getCurrentSession();
    tx=session.beginTransaction();
    hql= "from Users where username=? and password=? ";
    Query query=session.createQuery(hql);
    query.setParameter(0, u.getUsername());
    query.setParameter(1, u.getPassword());
    List list=query.list();
    tx.commit();
    if(list.size()>0){
     return true;
    }else{
     return false;
    }
   }
   catch(Exception ex){
    ex.printStackTrace();
    return false;
   }
   finally{
    if(tx!=null){
     
      //tx.commit();
      tx=null;
    }
   }
  }

 }

* 測試代碼

 package service.impl;


 import junit.framework.Assert;

 import org.junit.Test;

 import service.UsersDAO;
 import entity.Users;

 public class TestUserDAOImpl {

  @Test
  public void testUserLogin(){
   
   Users u=new Users(1,"zhangsan","123456");
   UsersDAO udao=new UsersDAOImpl();
   Assert.assertEquals(true, udao.usersLogiin(u));
   
  }
 }

微笑微笑微笑

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章