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));
   
  }
 }

微笑微笑微笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值