我的应用程序有问题,我正在尝试将一个新创建的对象保存到另一个中,在 1.st 我创建了 1.st 对象 o(订单),这可以正常工作:
tx = session.beginTransaction();
Orders o = new Orders();
o.setAccounts((Accounts) session.load(Accounts.class, id));
o.setDatumObjednavky(datum_obj);
o.setDoplnujuceUdaje(doplnujuce_udaje);
Integer oid = (Integer) session.save(o);
tx.commit();
但是当我尝试将 Orders o 对象分配给新创建的对象 pbc 时,在调用 session.save(pbc) 之后
tx = session.beginTransaction();
Orders o = new Orders();
o.setAccounts((Accounts) session.load(Accounts.class, id));
o.setDatumObjednavky(datum_obj);
o.setDoplnujuceUdaje(doplnujuce_udaje);
Integer oid = (Integer) session.save(o);
for (int i = 0; i < list.size(); i++) {
PiecesByCategory pbc = new PiecesByCategory();
pbc.setOrders((Orders) session.load(Orders.class, oid));
pbc.setCategory((Category) session.load(Category.class, list.get(i).getKategoria()));
pbc.setPocet(list.get(i).getPocet());
session.save(pbc);
System.out.println("test\n");
}
tx.commit();
它在 session.save(pbc) 处崩溃:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`postcomfort`.`pieces_by_category`, CONSTRAINT `pieces_by_category_ibfk_1` FOREIGN KEY (`fk_order`) REFERENCES `orders` (`id`))
我有 MySql 表 auto_increment、生成器类型标识、hybernate 映射文件和 java pojo,见下文,我做错了什么?感谢您的帮助
我的 hibernate.cfg.xml 文件:
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/postcomfort</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- <property name="hibernate.jdbc.use_get_generated_keys">true</property>-->
<!--<property name="hibernate.jdbc.batch_size">50</property>-->
<property name="hibernate.connection.autocommit">false</property>
<mapping resource="hlavny/Accounts.hbm.xml"/>
<mapping resource="hlavny/Role.hbm.xml"/>
<mapping resource="hlavny/LoadingParam.hbm.xml"/>
<mapping resource="hlavny/Orders.hbm.xml"/>
<mapping resource="hlavny/DocAccount.hbm.xml"/>
<mapping resource="hlavny/DocOrders.hbm.xml"/>
<mapping resource="hlavny/PiecesByCategory.hbm.xml"/>
<mapping resource="hlavny/Category.hbm.xml"/>
我的 Category.hbm.xml 文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 1.10.2012 10:02:17 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="hlavny.Category" table="category" catalog="postcomfort">
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="kategoria" type="string">
<column name="kategoria" length="300" not-null="true" />
</property>
<set name="piecesByCategories" inverse="true">
<key>
<column name="fk_category" not-null="true" />
</key>
<one-to-many class="hlavny.PiecesByCategory" />
</set>
</class>
</hibernate-mapping>
我的 java pojo Category.java 文件:
package hlavny;
// Generated 1.10.2012 10:02:17 by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
import java.util.Set;
/**
* Category generated by hbm2java
*/
public class Category implements java.io.Serializable {
private int id;
private String kategoria;
private Set<PiecesByCategory> piecesByCategories = new HashSet<PiecesByCategory>(0);
public Category() {
}
public Category(/*int id,*/ String kategoria) {
// this.id = id;
this.kategoria = kategoria;
}
public Category(/*int id, */String kategoria, Set<PiecesByCategory> piecesByCategories) {
// this.id = id;
this.kategoria = kategoria;
this.piecesByCategories = piecesByCategories;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getKategoria() {
return this.kategoria;
}
public void setKategoria(String kategoria) {
this.kategoria = kategoria;
}
public Set<PiecesByCategory> getPiecesByCategories() {
return this.piecesByCategories;
}
public void setPiecesByCategories(Set<PiecesByCategory> piecesByCategories) {
this.piecesByCategories = piecesByCategories;
}
}