Hibernate 判断对象是否包含一个字段

主要内容来源:
http://blog.csdn.net/zhangjk1993/article/details/40020813

applicationContext.xml

<bean id="hibernateConfigurationUtil" class="com.cn.test.hb.HibernateConfigurationUtil"></bean>

HibernateConfigurationUtil

package com.cn.test.hb;

import java.util.Iterator;

import org.hibernate.MappingException;
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;
        try{
            property = persistentClass.getProperty(propertyName);
        }
        catch(MappingException e)
        {
            return null;
        }
        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;
    }
}

使用

String column =  HibernateConfigurationUtil .getColumnName(TestModel.class,'columnName');
if(column==null)//字段不存在
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程圈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值