我有如下的Hibernate配置。
CPU型号:
<?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">
<hibernate-mapping>
<class name="org.example.CPUModel" table="cpu_model">
<id name="cpuModelId" type="java.lang.Integer" unsaved-value="null">
<column name="cpu_model_id"/>
<generator class="identity"/>
</id>
<property name="name" type="java.lang.String">
<column length="32" name="name"/>
</property>
<set name="softwares" inverse="true" lazy="false" table="cpu_model_software" cascade="all-delete-orphan" order-by="cpu_sw_id desc">
<key>
<column name="cpu_model_id" not-null="true"/>
</key>
<one-to-many class="org.example.CPUModelSoftware"/>
</set>
</class>
</hibernate-mapping>
CPU型号软件
<?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">
<hibernate-mapping>
<class name="org.example.CPUModelSoftware" table="cpu_model_software">
<composite-id name="cpuModelSoftwarePK" class="org.example.CPUModelSoftwarePK">
<key-property name="cpuModelId" column="cpu_model_id" type="java.lang.Integer"/>
<key-property name="cpuSwId" column="cpu_sw_id" type="java.lang.Integer"/>
</composite-id>
<many-to-one class="org.example.CPUSoftware" insert="false" update="false" fetch="select" name="cpuSw">
<column name="cpu_sw_id" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
选择值和更新可以在此配置下正常工作,但插入一个全新的值却不行。
我收到此错误:
DEBUG Could not execute JDBC batch update [insert into exampledb.cpu_model_software (cpu_model_id, cpu_sw_id) values (?, ?)]
java.sql.BatchUpdateException: Column 'cpu_model_id' cannot be null
因此,Hibernate尝试进行插入操作,而无需先从数据库获取生成的cpu_model_id,然后再为CPUModelSoftwarePK对其进行设置(请参阅CPUModel Hibernate配置)。
因此,我该如何告诉Hibernate它首先应该插入cpu_model表。然后从那里获取ID并设置CPUModelSoftwarePK的ID并插入?
CPUModelSoftware / PK的数据库如下。
+----------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------+------+-----+---------+-------+
| cpu_model_id | int(11) | NO | MUL | NULL | |
| cpu_sw_id | int(11) | NO | MUL | NULL | |
+----------------+---------+------+-----+---------+-------+
和用于CPUModel
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| cpu_model_id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(32) | NO | | | |
+----------------+------------------+------+-----+---------+----------------+
编辑
制作新的CPUModelSoftware时,似乎并没有使CPUModelSoftwarePK无效。但是现在我得到这个:
ids for this class must be manually assigned before calling save(): org.example.CPUModelSoftware
最佳答案
据我了解,Hibernate在hbm.xml
中定义的映射中不支持派生身份。这意味着Hibernate无法根据cpuModelId
和CPUModel
之间的关系自动设置CPUModelSoftware
的值。
因此,您需要首先保存CPUModel
(具有空的softwares
),然后需要将其生成的标识符分配给CPUModelSoftware
组合ID的字段,然后保存CPUModelSoftware
。
关于java - hibernate -在另一个类中进行fk自引用时处理添加(插入),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6107691/