本文参考自http://blog.csdn.net/fhwbj/article/details/3267787
在使用Hibernate时,我们有时可能需要根据实体类获得数据库表的信息,上面的那个链接的内容已经给出了模版,不过使用Hibernate4时,如果获得的PersistentClass一直为null,可以试着采用下面的方式初始化Configuration
- if (configuration == null) {
- configuration = new Configuration().configure();
- configuration.buildSessionFactory();
- }
HiberanteConfigurationUtil类 (映射工具类)
这里需要注意的是:
1.在取sessionFactory时要加上&号,原因可以看一下http://blog.csdn.net/zhangjk1993/article/details/40017583
2.这里采用的xml文件做的映射,要保证实体类名和对应的映射文件名一致
- package util;
- import java.util.Iterator;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.mapping.Column;
- import org.hibernate.mapping.PersistentClass;
- import org.hibernate.mapping.Property;
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
- /**
- * 根据实体类得到对应的表名、主键名、字段名(与Spring集成)
- * 这里使用xml文件配置的映射,需要保证实体类名与对应映射文件名一致,即User.java与User.hbm.xml
- * </p>
- * 这里使用继承ApplicationContextAware的方式来获得ApplicationContext,
- * 因此需要在Spring配置文件中配置一下该类,才能自动注入ApplicationContext对象
- *
- * <bean class="util.HibernateConfigurationUtil"/>
- */
- public class HibernateConfigurationUtil implements ApplicationContextAware {
- private static ApplicationContext applicationContext;
- private static Configuration configuration;
- public static Configuration getConfiguration() {
- if (configuration == null) {
- // 取sessionFactory的时候要加上&
- LocalSessionFactoryBean factory = (LocalSessionFactoryBean) applicationContext
- .getBean("&sessionFactory");
- configuration = factory.getConfiguration();
- }
- return configuration;
- }
- private static <T> PersistentClass getPersistentClass(Class<T> clazz) {
- synchronized (HibernateConfigurationUtil.class) {
- PersistentClass pc = getConfiguration().getClassMapping(
- clazz.getSimpleName());
- if (pc == null) {
- configuration = configuration.addClass(clazz);
- pc = configuration.getClassMapping(clazz.getName());
- }
- return pc;
- }
- }
- /**
- * 获得实体类对应的表名
- *
- * @param clazz
- * 实体类的Class对象
- * @return 表名
- */
- public static <T> String getTableName(Class<T> clazz) {
- return getPersistentClass(clazz).getTable().getName();
- }
- /**
- * 获得实体类对应表的主键字段名称
- *
- * @param clazz
- * 实体类的Class对象
- * @return 主键字段名称
- */
- public static <T> String getPKColumnName(Class<T> clazz) {
- return getPersistentClass(clazz).getTable().getPrimaryKey()
- .getColumn(0).getName();
- }
- /**
- * 获得类属性对应的字段名
- *
- * @param clazz
- * 实体类的Class对象
- * @param propertyName
- * 实体类的属性名
- * @return 属性对应的字段名
- */
- public static <T> String getColumnName(Class<T> clazz, String propertyName) {
- String columnName = "";
- PersistentClass persistentClass = getPersistentClass(clazz);
- Property property = persistentClass.getProperty(propertyName);
- Iterator<?> iterator = property.getColumnIterator();
- if (iterator.hasNext()) {
- Column column = (Column) iterator.next();
- columnName += column.getName();
- }
- return columnName;
- }
- @Override
- public void setApplicationContext(ApplicationContext context)
- throws BeansException {
- applicationContext = context;
- }
- }
- <span style="white-space:pre"> </span><bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>bean/User.hbm.xml</value>
- </list>
- </property>
- </bean>
- <bean class="util.HibernateConfigurationUtil" />
User类
- package bean;
- public class User {
- private int id;
- private String username;
- private String password;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- 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;
- }
- }
- <?xml version='1.0' encoding='utf-8'?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="bean.User" table="_user">
- <id name="id" type="int">
- <column name="id" />
- <generator class="identity" />
- </id>
- <property name="username" type="string">
- <column name="_username" />
- </property>
- <property name="password" type="string">
- <column name="_password" />
- </property>
- </class>
- </hibernate-mapping>
- new ClassPathXmlApplicationContext("applicationContext.xml");
- System.out.println(HibernateConfigurationUtil.getTableName(User.class));
- System.out.println(HibernateConfigurationUtil.getPKColumnName(User.class));
- System.out.println(HibernateConfigurationUtil.getColumnName(User.class,"username"));
版权声明:本文为博主原创文章,未经博主允许不得转载。