[英]Hibernate: How to cascade embedded objects with parent object's auto-generated ID as a foreign key
題:
在Hibernate(JPA 2.0)中,創建父對象時如何創建具有@OneToOne關系的嵌入式對象?
期望:
當我創建用戶時,我希望它會自動創建適當的屬性,並將用戶的自動生成的id
作為屬性的object_id
。
現實:
當我嘗試創建用戶時,出現以下異常:
javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.api.user.PropertyEntity.objectId
數據庫表:
我有兩個MySQL表: users
和properties
users
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1 | foo | bar |
+----+----------+----------+
| 2 | goo | baz |
+----+----------+----------+
| 3 | woo | hah |
+----+----------+----------+
properties
+----+-----------+-------------+---------+-----------+
| id | object_id | object_type | propkey | propvalue |
+----+-----------+-------------+---------+-----------+
| 1 | 1 | user | sso | true |
+----+-----------+-------------+---------+-----------+
| 2 | 1 | user | ssoid | foobar |
+----+-----------+-------------+---------+-----------+
| 3 | 2 | user | sso | false |
+----+-----------+-------------+---------+-----------+
| 4 | 2 | user | ssoid | null |
+----+-----------+-------------+---------+-----------+
| 5 | 3 | user | sso | false |
+----+-----------+-------------+---------+-----------+
| 6 | 3 | user | ssoid | null |
+----+-----------+-------------+---------+-----------+
Java實體:
一個用戶可以具有多個屬性,但是每個屬性都與該用戶具有@OneToOne關系,並且該關系是單向的(即,用戶創建其屬性,而不是相反)。
Java中的User
實體如下所示:
@Entity
@Table(name = "users")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "username", nullable = false)
private String userName;
@Column(name = "password", nullable = false)
private String password;
@OneToOne
@Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE })
@JoinColumnsOrFormulas(value={
@JoinColumnOrFormula(column=
@JoinColumn(name = "id", referencedColumnName = "object_id", insertable=false, updatable=false, nullable = false)
),
@JoinColumnOrFormula(formula=
@JoinFormula(referencedColumnName="object_type", value="'user'")
),
@JoinColumnOrFormula(formula=
@JoinFormula(referencedColumnName="propkey", value="'sso'")
)
})
private PropertyEntity isSso;
@OneToOne
@Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE })
@JoinColumnsOrFormulas(value={
@JoinColumnOrFormula(column=
@JoinColumn(name = "id", referencedColumnName = "object_id", insertable=false, updatable=false, nullable = false)
),
@JoinColumnOrFormula(formula=
@JoinFormula(referencedColumnName="object_type", value="'user'")
),
@JoinColumnOrFormula(formula=
@JoinFormula(referencedColumnName="propkey", value="'ssoid'")
)
})
private PropertyEntity ssoId;
// Getters & Setters
}
Property
實體如下所示:
@Entity
@Table(name = "properties")
public class PropertyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "object_id", nullable = false)
private Integer objectId;
@Column(name = "object_type", nullable = false)
private String objectType;
@Column(name = "propkey", nullable = false)
private String name;
@Column(name = "propvalue", nullable = false)
private String value;
// Getters & Setters
}
像大多數與Hibernate注釋有關的問題一樣,我通過完全消除它們並以老式的方式來解決此問題(在我的事務中進行單獨的查詢以創建/更新用戶,然后創建/更新每個用戶屬性記錄)。
如果任何人都有可以使用的Hibernate解決方案,我很樂意接受該答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.