休眠-当fk自引用在另一个类中时,句柄添加(插入)

| 我有如下的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不能根据
CPUModel
和ѭ9relationship之间的关系自动设置
cpuModelId
的值。 因此,您需要先保存
CPUModel
(带有空的
softwares
),然后需要将其生成的标识符分配给
CPUModelSoftware
的综合ID字段,并保存
CPUModelSoftware
。     

要回复问题请先登录注册