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
posted @ 2019-08-20 14:10  xyfaneast  阅读(758)  评论(0编辑  收藏  举报