Hibernate: insert into TB_CLOTHES (STATUS, COLOR, SIZE, BEFOREPRICE, PROMOTIONSPRICE, CLOTHESLENGTH, MODELTYPE, COLLARTYPE, STYLE, CLOTHESNAME, BRAND, FABRICTYPE, FABRICMATERIAL, FILLER, PROCESS, STYLEDETAILS, LININGMATERIAL, FUNCTIONAL, FITCROWD, ALREADYSALES, PICTURE, CLOTHESAMOUNT, BROWSEAMOUNT, CITY, COLLECTIONPOPULARITY, WHETHEROFFERS, CLOTHESPOINTS, SUPPLYTIMES, CLOTHESTYPE_ID, ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update TB_CLOTHESTYPE set CLOTHESTYPE=? where ID=?
控制台只给出以上信息下面是我配置的事务信息
<!-- 事务声明 -->
<bean id="txtManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="txtManager">
<tx:attributes>
<tx:method name="save*" />
<tx:method name="insert*" />
<tx:method name="update*" />
<tx:method name="delete*" />
<tx:method name="get*" read-only="true" propagation="SUPPORTS" />
<tx:method name="find*" read-only="true" propagation="SUPPORTS" />
<tx:method name="sum*" read-only="true" propagation="SUPPORTS" />
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置切点 -->
<aop:config>
<aop:pointcut id="methodsPointCut" expression="execution(* main.ch.service.*.*(..))"/>
<aop:advisor pointcut-ref="methodsPointCut" advice-ref="txAdvice"/>
</aop:config>
数据就是存入不了数据库,请问大家如何解决?

解决方案 »

  1.   

    这个是我dao层代码
    package ch.dao.hibernateimpl;import java.util.Date;
    import java.util.List;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.Criteria;
    import org.hibernate.Session;
    import org.hibernate.criterion.Expression;
    import org.hibernate.criterion.Order;
    import org.hibernate.criterion.Restrictions;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;//import ch.dao.hibernateImpl.news.DateUtil;public class BaseHibernateDao extends HibernateDaoSupport {
    protected final Log log = LogFactory.getLog(this.getClass());

    /**
     * Persist the given transient instance
     *  
     * @param entity the transient instance to persist
     */
    public void insertObject(Object entity) {
    getHibernateTemplate().save(entity);
    } /**
     * Update the given persistent instance
     * 
     * @param entity the persistent instance to update
     */
    public void updateObject(Object entity) {
    getHibernateTemplate().update(entity);
    } /**
     * Return the persistent instance of the given entity class 
     * with the given identifier, or null if not found.
     * 
     * @param entityClass a persistent class
     * @param id    an identifier of the persistent instance 
     * @return  the persistent instance, or null if not found 
     */
    @SuppressWarnings("unchecked")
    public Object getObject(Class entityClass, String id) {
    Object result = getHibernateTemplate().get(entityClass, id);
    return result;
    } /**
     * Delete the given persistent instance.
     *  
     * @param entity the persistent instance to delete
     */
    public void deleteObject(Object entity) {
    getHibernateTemplate().delete(entity);
    }

    /**
     * Delete the persistent instance of the given entity class with the given identifier.
     * 
     * @param entityClass a persistent class
     * @param id     an identifier of the persistent instance
     */
    // public boolean deleteObject(Class entityClass, String id) {
    // boolean deletable = false;
    // Object obj = getHibernateTemplate().get(entityClass, id);
    // getHibernateTemplate().delete(obj);
    // deletable = true;
    // return deletable;
    // }

    /**
     * Return all persistent instances of the given entity class.
     * 
     * @param entityClass a persistent class 
     * @return        a List containing 0 or more persistent instances
     */
    @SuppressWarnings("unchecked")
    public List findObjects(Class entityClass) {
    return getHibernateTemplate().loadAll(entityClass);
    }

    /**
     * Return the List of the persistent entities between the statrIndex and 
     * the endIndex with the given entity class.
     *  
     * @param startIndex   the started index
     * @param endIndex     the ended index
     * @param entityClass       a persistent class
     * @return    a List containing 0 or more persistent instances
     */
    @SuppressWarnings("unchecked")
    public List findObjects(int startIndex, int endIndex, Class entityClass) {
    List objects = super.getSession().createCriteria(entityClass)
     .setFirstResult(startIndex)
     .setMaxResults(endIndex - startIndex + 1)
     .list();
    return objects;
    }

    /**
     * Return the List of the persistent entities between the statrIndex and 
     * the endIndex with the given HQL query statement.
     *  
     * @param startIndex    the started index
     * @param endIndex     the ended index
     * @param hql        a HQL query statement
     * @return         a List containing 0 or more persistent instances
     */
    @SuppressWarnings("unchecked")
    public List findObjects(int startIndex, int endIndex, String hql) {
    List objects = super.getSession().createQuery(hql)
    .setFirstResult(startIndex)
    .setMaxResults(endIndex - startIndex + 1)
    .list();
    return objects;
    }

    /**
     * Get a Hibernate Session, either from the current transaction or a new one.
     * 
     * @return a Hibernate Session
     */
    public Session getHibernateSession() {
    return super.getSession();
    }

    /**
     * Return the total number with the specified HQL query statement
     * 
     * @param queryString a HQL query statement
     * @return total number or 0
     */
    public int sumObjects(String queryString) {
    return Integer.parseInt(super.getSession().createQuery(queryString)
    .uniqueResult().toString());
    }

    public void mergeObject(Object entity) {
    getHibernateTemplate().merge(entity);
    }

    @SuppressWarnings("unchecked")
    public int sumObjects(String type, Date sDate, Date eDate, Class entityClass) {
    Criteria criteria = super.getSession().createCriteria(entityClass); criteria.add(Expression.like("resourceType", "%" + type + "%"));

    if (sDate != null && eDate != null) {
    criteria.add(Restrictions.ge("creationDate", sDate));
    criteria.add(Restrictions.le("creationDate", eDate));
    } else if (sDate != null) {
    criteria.add(Restrictions.ge("creationDate", sDate));
    criteria.add(Restrictions.le("creationDate", DateUtil.getCurrentDateTime()));
    } else if (eDate != null) {
    criteria.add(Restrictions.le("creationDate", eDate));
    }

    criteria.addOrder(Order.desc("creationDate"));

    List result = criteria.list();

    return result.size();
    }
    }package ch.dao.hibernateimpl;import org.apache.log4j.LogManager;
    import org.apache.log4j.Logger;
    import org.hibernate.HibernateException;
    import org.springframework.util.StringUtils;import ch.dao.inf.IClothes;
    import ch.entity.Clothes;public class ClothesDaoHibernateImpl extends BaseHibernateDao implements
    IClothes {

    // **处理运行时,抛出的系统异常,并把 这些文件记录日志里
    protected Logger log = LogManager.getLogger(ClothesDaoHibernateImpl.class);

    public void insertClothes(Clothes clothes) {
    super.insertObject(clothes);
    }

    public void updateClothes(Clothes clothes) {
    super.updateObject(clothes); }

    public boolean deleteClothes(String[] clothes) {
    boolean success  = false;
    String p = StringUtils.arrayToCommaDelimitedString(clothes);

    String[] temp = p.split(",");

    String strIds = "";

    for (int i = 0; i < temp.length; i++) {
    if (i == temp.length - 1) {
    strIds += "'" + temp[i] + "'";
    } else {
    strIds += "'" + temp[i] + "',";
    }
    }
    String hql = "delete Clothes where id in (" + strIds + ") ";

    try {
    super.getHibernateSession().createQuery(hql).executeUpdate();
    success = true;
    } catch (HibernateException e) {
    e.printStackTrace();
    }
    return success;
    }

    public Clothes getClothes(String id) {
    return (Clothes) super.getObject(Clothes.class, id);
    } public int sumClothes(String clothesId) {

    return (int) super
    .sumObjects("select count(*) from Clothes where clothes.id = '"
    + clothesId + "'");
    }

    }
      

  2.   

    <!-- 配置事务 -->
    <!-- 事务管理实体 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <!-- 事务配置 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- 查询方法只读 -->
    <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
    <!-- 其它方法使用普通事务 -->
    <tx:method name="insert*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    </tx:attributes>
    </tx:advice>

    <!-- aop切入 -->
    <aop:config>
    <!--  AspackJ语言 -->
    <aop:pointcut expression="execution(* main.ch.service.*.*(..))" id="interCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="interCut"/>
    </aop:config>
    给你个例子
      

  3.   

    直接在程序中定义事物看看:
    Trancation tx = session.beginTrancation();
    getHibernateTemplate().update(entity); tx .commit();
      

  4.   

    那就直接去你的dao方法写个事务去
    Transaction tr=this.getSession().beginTransaction();
    ......
    tr.commit();
      

  5.   

    看看hibernate的配置有没有错误
      

  6.   

    我直接Transaction tr=this.getSession().beginTransaction();
    ......
    tr.commit();
    还是不行
      

  7.   


    把hibernate的配置贴出来看看
      

  8.   

    下面是我的hibernate.cfg.xml文件
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
      <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/yifuwang</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="ch/entity/Authority.hbm.xml"/>
        <mapping resource="ch/entity/Clothes.hbm.xml"/>
        <mapping resource="ch/entity/ClothesType.hbm.xml"/>
        <mapping resource="ch/entity/Favorites.hbm.xml"/>
        <mapping resource="ch/entity/Form.hbm.xml"/>
        <mapping resource="ch/entity/Manager.hbm.xml"/>
        <mapping resource="ch/entity/ManagerRole.hbm.xml"/>
        <mapping resource="ch/entity/Role.hbm.xml"/>
        <mapping resource="ch/entity/RoleAuthority.hbm.xml"/>
        <mapping resource="ch/entity/ShoppingCar.hbm.xml"/>
        <mapping resource="ch/entity/User.hbm.xml"/>
        <mapping resource="ch/entity/UserClothes.hbm.xml"/>
      </session-factory>
    </hibernate-configuration>
      

  9.   

    下面是我的applicationContext.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/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


    <!-- SPRING 集成hibernate --> <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
    value="classpath:hibernate.cfg.xml">
    </property>
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">

    <property name="driverClass">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="jdbcUrl">
    <value>jdbc:mysql://localhost:3306/yifuwang</value>
    </property>
    <property name="user">
    <value>root</value>
    </property>
    <property name="password">
    <value>root</value>
    </property>
    </bean>

    <!-- 事务声明 -->
    <bean id="txtManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txtManager">
    <tx:attributes>
    <tx:method name="save*" propagation="SUPPORTS"/>
    <tx:method name="insert*" propagation="SUPPORTS"/>
    <tx:method name="update*" propagation="SUPPORTS"/>
    <tx:method name="delete*" propagation="SUPPORTS"/>
    <tx:method name="get*" read-only="true" propagation="SUPPORTS" />
    <tx:method name="find*" read-only="true" propagation="SUPPORTS" />
    <tx:method name="sum*" read-only="true" propagation="SUPPORTS" />
    <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>
    <!-- 配置切点 -->
    <aop:config>
    <aop:pointcut id="methodsPointCut" expression="execution(* main.ch.service.*.*(..))"/>
    <aop:advisor pointcut-ref="methodsPointCut" advice-ref="txAdvice"/>
    </aop:config>
    </beans>
      

  10.   

    把 dataSource去掉看看<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"><property name="driverClass">
    <value>com.mysql.jdbc.Driver </value>
    </property>
    <property name="jdbcUrl">
    <value>jdbc:mysql://localhost:3306/yifuwang </value>
    </property>
    <property name="user">
    <value>root </value>
    </property>
    <property name="password">
    <value>root </value>
    </property>
    </bean>