码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate之AbstractEntityPersister

时间:2014-12-20 02:08:17      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

  1. <?xml version=‘1.0‘ encoding=‘utf-8‘?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.     <session-factory>  
  8.         <!-- Database connection settings -->  
  9.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  10.         <property name="connection.url">jdbc:mysql://localhost:3306/ehome</property>  
  11.         <property name="connection.username">root</property>  
  12.         <property name="connection.password">mysql</property>  
  13.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  14.         <property name="current_session_context_class">thread</property>  
  15.         <property name="show_sql">true</property>  
  16.   
  17.         <mapping resource="org/bkgd/ehome/jeeplugin/userapp/domain/HhArticle.hbm.xml" />  
  18.   
  19.     </session-factory>  
  20. </hibernate-configuration>  


HhArticle.hbm.xml 文件在此省略


编写HibernateUtil辅助类 获取sessionFactory

  1. public class HibernateUtils {  
  2.       
  3.     private static final SessionFactory sessionFactory;;  
  4.     //创建sessionFactory  
  5.     static {  
  6.         try {  
  7.             sessionFactory = new Configuration().configure().buildSessionFactory();  
  8.         } catch (Throwable ex) {  
  9.             System.err.println("Initial SessionFactory creation failed." + ex);  
  10.             throw new ExceptionInInitializerError(ex);  
  11.         }  
  12.     }  
  13.     public static SessionFactory getSessionFactory() {  
  14.         return sessionFactory;  
  15.     }  
  16. }  


测试类

    1. public class TestPersister {  
    2.     private static AbstractEntityPersister classMetadata;  
    3.     private Object obj;  
    4.       
    5.     public TestPersister(SessionFactory sessionFatory, Object obj) {  
    6.         classMetadata = (AbstractEntityPersister) sessionFatory.getClassMetadata(obj.getClass());  
    7.         this.obj = obj;  
    8.     }  
    9.     /** 
    10.      * @param args 
    11.      */  
    12.     public static void main(String[] args) {  
    13.         //实例化classMetadata   传入sessionFactory和obj对象  
    14.         TestPersister tp = new TestPersister(HibernateUtils.getSessionFactory(),new HhArticle());  
    15.         Session session = HibernateUtils.getSessionFactory().getCurrentSession();  
    16.         session.beginTransaction();  
    17.         SQLQuery sqlquery = session.createSQLQuery("DELETE FROM " + classMetadata.getTableName());  
    18.         sqlquery.executeUpdate();  
    19.         session.getTransaction().commit(); //事务提交  
    20.           
    21.         //classMetadata一些其他操作  
    22.         System.out.println(classMetadata.getTableName());  //表名  
    23.         System.out.println(classMetadata.getIdentifierPropertyName()); //主键列映射属性  
    24.         System.out.println(classMetadata.getEntityName()); //对象名称  
    25.         for(String str:classMetadata.getPropertyNames()){   //非主键属性   
    26.             System.out.println(str);     
    27.         }  
    28.     }  
    29.     public AbstractEntityPersister getClassMetadata() {  
    30.         return classMetadata;  
    31.     }  
    32.     public void setClassMetadata(AbstractEntityPersister classMetadata) {  
    33.         this.classMetadata = classMetadata;  
    34.     }  
    35.     public Object getObj() {  
    36.         return obj;  
    37.     }  
    38.     public void setObj(Object obj) {  
    39.         this.obj = obj;  
    40.     }  

Hibernate之AbstractEntityPersister

标签:

原文地址:http://www.cnblogs.com/yuyanbian/p/4174963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!