zoukankan      html  css  js  c++  java
  • hibernate的save方法和原生的sql语句使用的数据库区别

    hibernate配置如下

    urapport_config.url=jdbc:mysql://10.10.202.152:3307/urapport_config_inner?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8mb4&autoReconnect=true
    urapport_config.user=pgm
    urapport_config.password=pgmfetion
    <bean id="dataSource0_urapport_config" parent="abstractDatasource" >
            <property name="maxPoolSize">
                <value>${c3p0_r.maxPoolSize}</value>
            </property>
            <property name="jdbcUrl" value="${urapport_config.url}" />
            <property name="user" value="${urapport_config.user}" />
            <property name="password" value="${urapport_config.password}" />
        </bean>
    <bean id="sessionFactory_urapport_config" parent="sessionfactory" >
            <property name="dataSource">
                <ref bean="dataSource0_urapport_config"/> 
            </property>        
            <property name="packagesToScan">   
                <list>   
                    <value>com.osp.bean</value>
                </list>   
            </property>    
        </bean>
       
        <bean id="urapport_config" class="com.osp.dao.GenericDAO">
            <property name="sessionFactory" ref="sessionFactory_urapport_config" />
        </bean>

    实体类注解如下

    @Entity
    @Table(name = "cin_clientupdate_new", catalog = "urapport_config")
    public class ClientVersion {
        private Long id;
        private String clienttype;
        private String clientversion;
        private String updatetype;
    }

    使用save方法保存对象时

    public void save(ClientVersion version){
            this.genericDAO("urapport_config").save(version);
        }
    public GenericDAO genericDAO(String daoName) {
            GenericDAO dao = (GenericDAO) SpringHelper.getBean(daoName);
            logger.debug(daoName);
            return dao;
        }
    public T save(T obj) {
            getHibernateTemplate().save(obj);
            return obj;
        }
    此时保存到的数据库是实体类注解上的库中,即数据库urapport_config中。如果实体类注解不指定数据库,则使用hibernate配置的数据库。

    public void updateStatus(String status,long id){
            String sql = "update cin_clientupdate_new set updatetype =? where id =?";
            genericDAO("urapport_config").sqlUpdate(sql, status,id);
        }
    
    public int sqlUpdate(final String sql, final Object... objs) {
            Integer result = 0;
            result = (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws SQLException {
                    int updnum;
                    try {
                        SQLQuery query = session.createSQLQuery(sql);
                        if (objs != null && objs.length > 0) {
                            for (int i = 0; i < objs.length; i++) {
                                query.setParameter(i, objs[i]);
                            }
                        }
                        updnum = 0;
                        updnum = query.executeUpdate();
    
                    }catch (Exception e) {
                        e.printStackTrace();
                        return 0;
                    } finally {
                        releaseSession(session);
                    }
                    return updnum;
                }
            });
            return result;
        }

    执行原生sql时,使用的数据库是hibernate配置的数据库,即urapport_config_inner。

    使用注意:

    执行原生sql时,实体类建议不要用注解配置数据库,以免两处配置的不一样。会使用连接池中配置的数据库。

    执行hibernate提供的持久化方法时,会优先使用实体类注解配置的数据库。如果注解未配置,则使用连接池中配置的。

    urapport_config_inner
  • 相关阅读:
    PHP 5.5.0 Alpha5 发布
    Ubuntu Touch 只是另一个 Android 皮肤?
    MariaDB 10 已经为动态列提供文档说明
    Percona Toolkit 2.1.9 发布,MySQL 管理工具
    Oracle Linux 6.4 发布
    Ruby 2.0.0 首个稳定版本(p0)发布
    Apache Pig 0.11.0 发布,大规模数据分析
    Node.js 0.8.21 稳定版发布
    红薯 MySQL 5.5 和 5.6 默认参数值的差异
    Django 1.5 正式版发布,支持 Python 3
  • 原文地址:https://www.cnblogs.com/xyfaneast/p/11382576.html
Copyright © 2011-2022 走看看