仅在辅助 table 中保留值
Persist value only in secondary table
任何人都有如何仅在次要 table 中保留值的示例?
@Entity
@Named("account")
@Table(name = "ACCOUNT", uniqueConstraints = {})
@SecondaryTables({ @SecondaryTable(name = "ACCOUNTCOMMENT", pkJoinColumns = { @PrimaryKeyJoinColumn })) {
...
@Column(table = "ACCOUNTCOMMENT", name = "COMMENTS", unique = false, nullable = true, insertable = true, updatable = true, length = 4000)
public String getComment() {
return this.comment;
}
public void setComment(String comments) {
this.comment = comments;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(table = "ACCOUNTCOMMENT", name = "LAST_EDITED", unique = false, nullable = true, insertable = true, updatable = true, length = 7)
public Date getCommentLastEdited() {
return this.commentLastEdited;
}
public void setCommentLastEdited(Date lastEdited) {
this.commentLastEdited = lastEdited;
}
@Column(table = "ACCOUNTCOMMENT", name = "LAST_EDITOR_EMPLOYEE_ID", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
public String getCommentLastEditorEmployeeId() {
return this.commentLastEditorEmployeeId;
}
public void setCommentLastEditorEmployeeId(String lastEditorEmployeeId) {
this.commentLastEditorEmployeeId = lastEditorEmployeeId;
}
....
}
我正在尝试仅更新辅助 table(AccountComment
) 中的值。我正在使用休眠 4 和 JPA2。
尝试了以下代码,
@Transactional
public void saveAccountComment() throws SystemException {
System.out.println("Entered into saveAccountComments method...");
Account acct = acctInfo.getAccount();
acct.setComment(accountForm.getCommentText());
Date lastEdited = new Date();
acct.setCommentLastEdited(lastEdited);
Officer lastEditor = utils.getOfficer();
acct.setCommentLastEditorEmployeeId(lastEditor.getEmployeeId());
accountForm.setLastEditDate(lastEdited);
accountForm.setLastEditor(lastEditor.getEmpIdAndFullName());
em.persist(acct);
}
它不起作用。也没有错误日志。当我调用 saveAccountComment()
方法时。 IT 卡住了。
有人知道如何仅在次要 table 中保留值吗?
我找到了解决问题的办法。以下代码有助于保留帐户对象。
@Transactional
public void saveAccountComment() throws SystemException {
Date lastEdited = new Date();
Officer lastEditor = utils.getOfficer();
accountForm.setLastEditDate(lastEdited);
accountForm.setLastEditor(lastEditor.getEmpIdAndFullName());
Query q = em.createNamedQuery("findAccountByAccountKey").setParameter("accountKey", acctInfo.getAccount().getAccountKey());
Account account = (Account) q.getSingleResult();
account.setCommentLastEdited(lastEdited);
account.setCommentLastEditorEmployeeId(lastEditor.getEmployeeId());
account.setComment(accountForm.getCommentText());
em.persist(account);
}
不是持久化帐户对象而是直接传递 id(在我的例子中是帐户密钥)并检索对象并设置值并持久化。
由于可以将多个独立的 class 映射到同一个 table,您可以将另一个单独的实体 class 映射到辅助 table(id作为连接列)。这样,您可以在只想对辅助 table 数据进行操作的情况下使用该实体。
但是,无论您采用何种方法,您都可能需要重新考虑当前的设计。如果需要分别处理主要和次要 table,最好根本不要使用次要 table,而是使用两个独立的关联实体。
任何人都有如何仅在次要 table 中保留值的示例?
@Entity
@Named("account")
@Table(name = "ACCOUNT", uniqueConstraints = {})
@SecondaryTables({ @SecondaryTable(name = "ACCOUNTCOMMENT", pkJoinColumns = { @PrimaryKeyJoinColumn })) {
...
@Column(table = "ACCOUNTCOMMENT", name = "COMMENTS", unique = false, nullable = true, insertable = true, updatable = true, length = 4000)
public String getComment() {
return this.comment;
}
public void setComment(String comments) {
this.comment = comments;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(table = "ACCOUNTCOMMENT", name = "LAST_EDITED", unique = false, nullable = true, insertable = true, updatable = true, length = 7)
public Date getCommentLastEdited() {
return this.commentLastEdited;
}
public void setCommentLastEdited(Date lastEdited) {
this.commentLastEdited = lastEdited;
}
@Column(table = "ACCOUNTCOMMENT", name = "LAST_EDITOR_EMPLOYEE_ID", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
public String getCommentLastEditorEmployeeId() {
return this.commentLastEditorEmployeeId;
}
public void setCommentLastEditorEmployeeId(String lastEditorEmployeeId) {
this.commentLastEditorEmployeeId = lastEditorEmployeeId;
}
....
}
我正在尝试仅更新辅助 table(AccountComment
) 中的值。我正在使用休眠 4 和 JPA2。
尝试了以下代码,
@Transactional
public void saveAccountComment() throws SystemException {
System.out.println("Entered into saveAccountComments method...");
Account acct = acctInfo.getAccount();
acct.setComment(accountForm.getCommentText());
Date lastEdited = new Date();
acct.setCommentLastEdited(lastEdited);
Officer lastEditor = utils.getOfficer();
acct.setCommentLastEditorEmployeeId(lastEditor.getEmployeeId());
accountForm.setLastEditDate(lastEdited);
accountForm.setLastEditor(lastEditor.getEmpIdAndFullName());
em.persist(acct);
}
它不起作用。也没有错误日志。当我调用 saveAccountComment()
方法时。 IT 卡住了。
有人知道如何仅在次要 table 中保留值吗?
我找到了解决问题的办法。以下代码有助于保留帐户对象。
@Transactional
public void saveAccountComment() throws SystemException {
Date lastEdited = new Date();
Officer lastEditor = utils.getOfficer();
accountForm.setLastEditDate(lastEdited);
accountForm.setLastEditor(lastEditor.getEmpIdAndFullName());
Query q = em.createNamedQuery("findAccountByAccountKey").setParameter("accountKey", acctInfo.getAccount().getAccountKey());
Account account = (Account) q.getSingleResult();
account.setCommentLastEdited(lastEdited);
account.setCommentLastEditorEmployeeId(lastEditor.getEmployeeId());
account.setComment(accountForm.getCommentText());
em.persist(account);
}
不是持久化帐户对象而是直接传递 id(在我的例子中是帐户密钥)并检索对象并设置值并持久化。
由于可以将多个独立的 class 映射到同一个 table,您可以将另一个单独的实体 class 映射到辅助 table(id作为连接列)。这样,您可以在只想对辅助 table 数据进行操作的情况下使用该实体。
但是,无论您采用何种方法,您都可能需要重新考虑当前的设计。如果需要分别处理主要和次要 table,最好根本不要使用次要 table,而是使用两个独立的关联实体。