编程语言
首页 > 编程语言> > java-仅在辅助表中保留值

java-仅在辅助表中保留值

作者:互联网

任何人都有如何仅在辅助表中保留值的示例?

@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;
}

....
}

我试图仅在辅助表(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()方法时.它只是卡住了.

有人知道如何仅将值保留在辅助表中吗?

解决方法:

我已经找到解决我问题的方法.以下代码有助于持久保存帐户对象.

@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);
}

而不是持久保存Account对象,而是直接传递ID(在我的情况下为account key)并检索对象并设置值并持久保存.

标签:hibernate,jpa-2-0,java,deltaspike-jpa
来源: https://codeday.me/bug/20191118/2029655.html