我有以下實體:嵌入式ID設置爲null
@Entity
@Table(name = "B_Message")
public class Message implements java.io.Serializable {
@Id
@GeneratedValue(generator = "app-uuid")
@Column(name = "MessageID", unique = true, nullable = false, length = 36, columnDefinition = "uniqueIdentifier")
private String id;
@OneToMany(cascade= {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY, mappedBy = "message")
private Set<MessageReceiver> messageReceivers;
}
和
@Entity
@Table(name="B_MessageReceiver")
public class MessageReceiver implements java.io.Serializable {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="messageId", [email protected](name="MessageID", nullable=false, length=36, columnDefinition = "uniqueIdentifier")),
@AttributeOverride(name="accountabilityId", [email protected](name="AccID", nullable=false, length=36, columnDefinition = "uniqueIdentifier")) })
private MessageReceiverId id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="MessageID", nullable=false, insertable=false, updatable=false)
private Message message;
}
和
@Embeddable
public class MessageReceiverId implements java.io.Serializable {
@Column(name = "MessageID", nullable = false, length = 36)
private String messageId;
@Column(name = "AccID", nullable = false, length = 36)
private String accountabilityId;
}
而且我在下面的代碼
Message message = new Message();
Set<MessageReceiver> messageReceivers = new HashSet<MessageReceiver>();
MessageReceiver messageReceiver = new MessageReceiver();
MessageReceiverId messageReceiverId = new MessageReceiverId();
messageReceiverId.setAccountabilityId(this.accountabilityModel.getId());
messageReceiver.setMessage(message);
messageReceiver.setId(messageReceiverId);
messageReceivers.add(messageReceiver);
messageManager.saveMessage(message);
使用它們
但是,當我想要合併Message
實體時,messageId
的MessageReceiverId
未被設置爲 並向數據庫發送空值。
的問題是,'message'是一個新創建的實體並且尚未保留,所以它沒有爲它自動創建任何消息ID。請注意,messageId應自動創建併合併到其他相關實體中。 – Soosh 2014-12-02 10:53:00
因此,在將其ID分配給'messageReceiverId'之前,必須將該消息對象保存在數據庫中。 – 2014-12-02 12:01:42
是的,因爲'messageId'是我自動生成的。 – Soosh 2014-12-02 12:08:11