映射文件见上一篇《hibernate 级联删除》
插入时候报错:
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not insert: [com.hotel.dao.Movie]; uncategorized SQLException for SQL [insert into hotel.movie (moviename, moviename_sys, director, impersonator, charge, intro, imgsrc, field1, field2) values (?, ?, ?, ?, ?, ?, ?, ?, ?)]; SQL state [HY000]; error code [1364]; Field 'id_movietype' doesn't have a default value; nested exception is java.sql.SQLException: Field 'id_movietype' doesn't have a default value
Caused by: java.sql.SQLException: Field 'id_movietype' doesn't have a default value
生成sql时id_movietype没有值,重新翻书查找了一下原因,是这里的多方的配置文件有问题:
配置了many-to-one 的 movietype 属性以后就应该删除idmovietype 属性。
修改后仍然是报上面的错误,再来查查hibernate的insert 属性文档:
写道
update, insert (optional - defaults to true): specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" association whose value is initialized from another property that maps to the same column(s), or by a trigger or other application.
将insert="false" 属性设置为 true,修改后的映射文件:
<?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="com.hotel.dao.Movie" table="movie" catalog="hotelfidelio"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native"></generator> </id> <property name="moviename" type="java.lang.String"> <column name="moviename" length="200" not-null="true" /> </property> <property name="movienameSys" type="java.lang.String"> <column name="moviename_sys" length="300" not-null="true" unique="true" /> </property> <property name="director" type="java.lang.String"> <column name="director" length="200" /> </property> <property name="impersonator" type="java.lang.String"> <column name="impersonator" length="200" /> </property> <property name="charge" type="java.lang.Double"> <column name="charge" precision="22" scale="0" not-null="true" /> </property> <property name="intro" type="java.lang.String"> <column name="intro" length="16277215" /> </property> <property name="imgsrc" type="java.lang.String"> <column name="imgsrc" length="300" /> </property> <property name="field1" type="java.lang.String"> <column name="field1" length="20" /> </property> <property name="field2" type="java.lang.String"> <column name="field2" length="20" /> </property> <many-to-one name="movietype" class="com.hotel.dao.Movietype" column="id_movietype" insert="true" update="false" outer-join="auto" fetch="select" access="property" not-null="true" lazy="false" > </many-to-one> </class> </hibernate-mapping>
对应的java代码:
Movie movie = new Movie();
movie.setIntro(intro);
movie.setCharge(Double.parseDouble(charge));
movie.setDirector(director);
movie.setImgsrc(movieimagename_);
movie.setImpersonator(impersonator);
movie.setIntro(intro);
movie.setMoviename(moviename);
movie.setMovienameSys(moviename);
Movietype mt = this.movieOperatorInterface.getMovieType(type);
movie.setMovietype(mt);
this.movieOperatorInterface.saveMovieInfo(movie);
重新测试,问题解决:
Hibernate: insert into hotel.movie (moviename, moviename_sys, director, impersonator, charge, intro, imgsrc, field1, field2, id_movietype) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)