我正在使用 Hibernate 保存与其他表有关系的表的记录,但我需要允许空引用,而不保存子对象。
情况是这样的:A类有相关的对象b和c,当我保存一个A实例时,我想保存b和c的空引用。
表结构如下:
id (PK) | id_tr_inner_costs | release_id | requirement_id | task_id | hours | total_cost
requirements_id 和/或 task_id 可以为空,在这种情况下应该这样设置。
可能吗?
谢谢
我正在使用 Hibernate 保存与其他表有关系的表的记录,但我需要允许空引用,而不保存子对象。
情况是这样的:A类有相关的对象b和c,当我保存一个A实例时,我想保存b和c的空引用。
表结构如下:
id (PK) | id_tr_inner_costs | release_id | requirement_id | task_id | hours | total_cost
requirements_id 和/或 task_id 可以为空,在这种情况下应该这样设置。
可能吗?
谢谢
Hibernate will set those ID's to null if the references are null at the time of saving. So something like this:
A instanceOfA = ...;
instanceOfA.setRelease(null);
instanceOfA.setTask(null);
session.save(instanceOfA);
should result in you having a row in the A table that has NULL for both release_id and task_id.
This is detailed very lightly in the Hibernate docs:
With the exception of collections, all built-in Hibernate types support null semantics.
ie, if something is null in Java, then it will be null in the database.