Persist value only in secondary table

1.3k Views Asked by At

Anyone have example of how to persist the values only in secondary 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;
}

....
}

I am trying to update the values only in the secondary table(AccountComment). I am using hibernate 4 and JPA2.

Tried the following code,

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

It is not working. There is no error log also. When I called the saveAccountComment() method. IT just stuck.

Anybody have idea how to persist the values only in secondary table?

2

There are 2 best solutions below

0
On BEST ANSWER

I have found the solution for my issue. The following code helped to persist the account object.

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

Instead of persisting the Account object directly pass the id (in my case account key) and retrieve the object and set the values and persist.

0
On

Since it is possible to have multiple independent classes mapped to the same table, you could map another separate entity class to the secondary table (id being the join column). This way you can use that entity in situations when you want to do operations on the secondary table data only.

However, regardless of the approach you take, you may want to reconsider your current design. If there is a need to treat primary and secondary tables separately, it may be better not to use secondary tables at all, but two separate associated entities.