为什么Hibernate在控制台中为单个查询显示两个不同的查询?`

时间:2016-05-24 06:19:41

标签: java hibernate

我是这个Hibernate框架的新手,并且只是使用Hibernate 3来开始使用这个框架。 我有一个小模块正在执行更新查询。

public void saveProduct(Product prod) {
    String hql = "UPDATE Product set description = :description, price = :price,ctr=ctr+1 WHERE id = :id";
    Query query = this.sessionFactory.getCurrentSession().createQuery(hql);

    query.setParameter("description", prod.getDescription());
    query.setParameter("price", prod.getPrice());
    query.setParameter("id", prod.getId());

    logger.info(prod.toString());
    int result = query.executeUpdate();
    logger.info("Rows affected: " + result);
}

这些是这个特定模块的日志,我在这部分有问题,hibernate显示两个不同的更新查询,一个没有列 ctr ,另一个列 ctr 或者是这是一种正常行为:

May 23, 2016 5:55:37 PM com.mogae.dashboard.db.dao.ProductDaoImp saveProduct
    INFO: Description: test311;Price: 1.7279999999999998
    Hibernate:
        update
            products
        set
            description=?,
            price=?
        where
            id=?
    Hibernate:
        update
            products
        set
            description=?,
            price=?,
            ctr=ctr+1
        where
            id=?
    May 23, 2016 5:55:37 PM com.mogae.dashboard.db.dao.ProductDaoImp saveProduct
    INFO: Rows affected: 1

这是我的数据库连接的xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <!-- the parent application context definition for the springapp application -->

    <aop:config>
        <aop:advisor pointcut="execution(* *..ProductManager.*(..))" advice-ref="txAdvice"/>
    </aop:config>

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <bean id="productDao" class="com.mogae.dashboard.db.dao.ProductDaoImp">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
                <value>product.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username"  value="${db.user}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>
</beans>

这是我的hibernate映射文件:

<?xml version="1.0"?>

<!DOCTYPE   hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.mogae.dashboard.actions.domain.Product" table="products" lazy="false">
        <id name="id" column="id" type="int">
            <generator class="native">
                <param name="sequence">products_id_seq</param>
            </generator>
        </id>

        <property name="description" column="description" type="string" />
        <property name="price" column="price" type="double" />
    </class>
</hibernate-mapping>

这是我的实体(产品)类:

   package com.mogae.dashboard.actions.domain;

import java.io.Serializable;

public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("Description: " + description + ";");
        buffer.append("Price: " + price);
        return buffer.toString();
    }

    private String description;
    private Double price;
    private int id;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

请帮助我一直在谷歌和各种hibernate论坛上搜索这个问题但是找不到。

0 个答案:

没有答案