Hibernate之AbstractEntityPersister

Hibernate开发中需要根据持久层对象获取所映射的数据库表相关信息,如表名,主键,外键对象等相关系。

Hibernate配置 Hibernate.cfg.xml

 

[html][/html] view plaincopy

  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. <hibernate-configuration>
  6.     <session-factory>
  7.         <!– Database connection settings –>
  8.         <property name=”connection.driver_class”>com.mysql.jdbc.Driver</property>
  9.         <property name=”connection.url”>jdbc:mysql://localhost:3306/ehome</property>
  10.         <property name=”connection.username”>root</property>
  11.         <property name=”connection.password”>mysql</property>
  12.         <property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>
  13.         <property name=”current_session_context_class”>thread</property>
  14.         <property name=”show_sql”>true</property>
  15.         <mapping resource=”org/bkgd/ehome/jeeplugin/userapp/domain/HhArticle.hbm.xml” />
  16.     </session-factory>
  17. </hibernate-configuration>

HhArticle.hbm.xml 文件在此省略

 

 

编写HibernateUtil辅助类 获取sessionFactory

 

[java][/java] view plaincopy

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

测试类

 

 

[java][/java] view plaincopy

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

标签