Hibernate会话更新无效

时间:2016-07-10 14:56:52

标签: java mysql hibernate

此处存储在MySQL数据库中的日期即使在执行更新查询后也不会更新。我通过设置show_sql = true检查了sql查询。这里HBHelper是一个具有静态方法来创建新会话的类。它与其他更新一起正常工作,但只有这个类有一些问题。

 package com.mbs.hibernate;
 import java.util.ArrayList;
 import org.hibernate.Query;
 import org.hibernate.Session;
 import com.mbs.model.Show;

public class UpdateShowAfterPayService {

  public boolean updateShow(String id, int count, ArrayList<Integer> pos) {
    // TODO Auto-generated method stub
    Session session = HBHelper.openSession(); // static methods which create session
    try {
        session.getTransaction().begin();
        Query q = session.createQuery("from Show where show_id=?");
        q.setString(0, id);
        Show show = (Show) (q.list()).get(0);
        StringBuilder sb = new StringBuilder(show.getCurrent_pattern());
        int booked = show.getBooked_seats();
        booked += count;
        for (int i = 0; i < pos.size(); i++) {
            sb.setCharAt(pos.get(i) ,'2');
        }
        System.out.println("booked.."+booked);
        show.setBooked_seats(booked);
        show.setCurrent_pattern(sb.toString()); 
        session.update(show);
        session.flush();
        session.getTransaction().commit();
        System.out.println("updated......");
        return true;
    } catch (Exception e) {
        System.out.println(e);
        return false;
    } finally {
        session.close();
    }

}
}

这是Show类的hbm.xml

     <hibernate-mapping>
    <class name="com.mbs.model.Show" table="shows">

    <id name="show_id" type="string" length="50">
        <column name="SHOW_ID" />
    </id>

    <property name="show_date" type="date">
        <column name="SHOW_DATE"></column>
    </property>

    <property name="show_time" type="time">
        <column name="SHOW_TIME"></column>
    </property>


    <property name="vip_cost" type="integer">
        <column name="VIP_COST"></column>
    </property>

    <property name="exe_cost" type="integer">
        <column name="EXE_COST"></column>
    </property>

    <property name="normal_cost" type="integer">
        <column name="NORMAL_COST"></column>
    </property>

    <property name="booked_seats" type="integer">
        <column name="BOOKED_SEATS" default="0"></column>
    </property>

    <property name="current_pattern" type="text">
        <column name="CUR_PATTERN"></column>
    </property>
    <many-to-one name="mov" class="com.mbs.model.Movie"
        column="MOVIE_ID" not-null="true" lazy="false" cascade="all"></many-to-one>

    <many-to-one name="audi" class="com.mbs.model.Auditorim"
        column="AUDI_ID" not-null="true" lazy="false" cascade="all"></many-to-one>

</class>
</hibernate-mapping>

0 个答案:

没有答案