ORM环境的构建以及基础的demo

hibernate配置文件

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

<hibernate-configuration>
 <session-factory>
  <property name="myeclipse.connection.profile">
   JDBC for Oracle
  </property>
  <!--URL地址-->
  <property name="connection.url">jdbc:oracle:thin:@localhost:1521:oracle</property>
  <!--用户名-->
  <property name="connection.username">scott</property>
  <!--密码-->
  <property name="connection.password">tiger</property>
  <!--驱动-->
  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="dialect"></property>
  <property name="dialect">
   org.hibernate.dialect.OracleDialect
  </property>
  <!--开发user表的映射文件User.hbm.xml-->
  <mapping resource="com/demo/hibernate/beans/User.hbm.xml" />
 </session-factory>
</hibernate-configuration>
<!--hibernate的配置文件-->

 

映射文件

<!--hibernate映射文件包含了对象/关系所需要的元数据  ,元数据包含持久化类的声明,和属性到数据库的映射
根元素hibernate-mapping,并通过package指定类所在的包,每一个表使用一个class定义,name属性表示类的名称,table表示关联的表名,通过property子元素来映射类的变量名与数据库表字段之间的映射关系-->
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-mapping package="com.demo.hibernate.beans">
 <class name="User" table="user">
  <id name="id" column="ID" type="integer">
   <generator class="native" />
  </id>
  <property name="username" column="username" type="string" />
  <property name="password" column="password" type="string" />
  <property name="email" column="email" type="string">
</hibernate>

持久化java类

public class User
{
 private java.lang.Integer id ;
 private java.lang.String username ;
 private java.lang.String password ;
 private java.lang.String email ;
};
//为属性字段声明访问器getter和setter
//提供一个标识属性
//使用非final的类

 

DAO类

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

import com.demo.hibernate.beans.User ;
public class UserDAO
{
 public User getUser() throws HibernateException
 {
  Session session = null ;
  Transaction tx = null ;
  User user = null ;
  try
  {
   session = HibernateSessinFactory.currentSession() ;//取得session
   tx = session.beginTransaction() ;
   Query query = session.createQuery(from User where username=?) ;
   query.setString(0,uername.trim()) ;
   user = (User)query.uniqueResult() ;
   query = null ;
   tx.commit() ;
  }
  catch (HibernateException e)
  {
   throw e ;
  }
  finally
  {
   if (tx != null)
   {
    tx.rollback() ;
   }
   HibernateSessionFactory.closeSession() ;
  }
  return user ;
 }
}

 

session工厂类

//这是一个持久化管理器,我们通过它从数据库中存取User
//SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory() ;
import org.Hibernate.HibernateException ;
import org.Hibernate.Session ;
import org.Hibernate.cfg.Configuration ;
public class HibernateSessionFactory
{
 private static String CONFIG_FILE_LOCATION = "/Hibernate.cfg.xml" ;
 private static final ThreadLocal threadLocal = new ThreadLocal() ;
 private static final Configuration cfg = new Configuration() ;
 private static org.hibernate.SessionFactory sessionFactory ;
 //取得session
 public static Session currentSession() throws HibernateException
 {
  Session session = (Session)threadLocal.get() ;//Session引入的是org.Hibernate.Session
  if (session == nll)
  {
   if (session == null)
   {
     try
    {
     cfg.configure(CONFIG_FILE_LOCATION) ;//通过对configure()的调用来装载Hibernate.cfg.xml配置文件,并初始化成一个Configuration实例    
     sessionFactory = cfg.buildSessionFactory() ;//?
    }
    catch (Exception e)
    {
     System.err.println("%%%%Error Creating SessionFactory%%%") ;
    }
   }
   session = sessionFactory.openSession() ;
   threadLocal.set(session) ;   
  }
  return session ;
 }
 //关闭session
 public static void closeSession() throws HibernateException
 {
  Session session = (Session)threadLocal.get() ;
  threadLocal.set(null) ;
  if (session != null)
  {
   session.close() ;
  }
 }
 
}

service类

import com.demo.hibernate.beans.User ;
public class UserService
{
 public boolean valid(String name,String word)
 {
  UserDAO test = new UserDAO() ;
  User user = test.getUser("admin") ;
  if (user.getPassword().equals(password))
  {
   return true ;
  }
  else
  {
   return false ;
  }
 }
 public static void main(String args[])
 {
  UserService service = new UserService() ;
  boolean login = service.valid("admin","admin") ;
  System.out.println("验证结果为:"+login) ;
 }
}
编程中遇到的问题:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: cn.ljq.hibernate.domain.User

解释:该异常原因是没有找映射文件

解决:Hibernate配置文件中加入此代码

<mapping resource="cn/ljq/hibernate/domain/User.hbm.xml"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值