日期:2014-05-20  浏览次数:20790 次

插入成功了,但还是报错!用hibernate插入数据
用hibernate插入数据,明明插入成功了,但还是报错!!为啥啊???
Java code
package hibernates;

import org.hibernate.Session;

public class UserDao {
    Session session=null;
        public String saveMovie(Movie movie){
        try{
            session=CreateSession.getSession();
            session.beginTransaction();
            session.save(movie);
            session.beginTransaction().commit();
            return "true";
        }catch(Exception e){
            session.getTransaction().rollback();
            return "false"+e.getLocalizedMessage();
        }finally{
            CreateSession.closeSession();
        }
    }
}


我试了下,是session.beginTransaction().commit();这句话报出的错!
错误:Could not execute JDBC batch update

但是明明成功插入数据了啊!!求解求解……高手给个解!!

下面是movie表的配置文件Movie.hbm.xml:
XML code

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="hibernates.Movie" table="movie" catalog="myweb">
        <id name="id" type="java.lang.String">
            <column name="id" length="45" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="45" />
        </property>
    </class>
</hibernate-mapping>


这个是hibernate配置文件:
XML code
<?xml version='1.0' encoding='UTF-8'?>
<!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="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/myweb</property>
    <property name="connection.username">root</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.password"></property>
    <property name="hbm2ddl.auto">true</property>

    <mapping resource="hibernates/Movie.hbm.xml" />

</session-factory>

</hibernate-configuration>


------解决方案--------------------
把你Movie.hbm.xml文件中的catalog="myweb"这个配置属性删掉看下,可能是这个属性导致的
------解决方案--------------------
对的,这个myweb应该是你的库名吧,catalog="myweb" 这个不要,
------解决方案--------------------
Transaction tx = session.beginTransaction();
session.save(movie);
tx.commit();

试试看。。
------解决方案--------------------
为什么不交给spring管理呢?
------解决方案--------------------
id如果是主键,必须把它配置成主键
<id name="id" type="integer" column="id">
<generator class="assigned" />
</id>
------解决方案--------------------