tags:

views:

315

answers:

1

I have a database that can have different types of relatinships based upon what data was updated during the last business transaction. An example would be in a policy writing application. The main table contains generic policy level information and there are related tables containing information such as the agent's information and the insureds information. When the first transaction is issued all of the tables are populated, however on subsequent transactions only the tables that have updates are persisted to the database. What I am trying to do now is to create classes implementing JPA that define the relationships between the tables as one-to-one. I would then like to implement custom loaders for each of the relationships to ensure that each time I retrieve the policy entity I can be able to retrieve the most recent version of the corresponding tables. Does anyone know how to implement this logic? I have tried using @NamedNativeQuery and using HQL, but in both instances it tells me that a column name I am specifying doesn't exist.

Any assistance would be greatly appreciated.

Code has been provided below to help illustrate what I am doing.

@Entity
@NamedNativeQuery(name = "loadLatestBilling",
                  query = "Select {i.*} from TransactionSummary as ts outer join BillingInfo i on i.systemAssignId=ts.systemAssignId where ts.systemAssignId=:systemAssignId and i.transSeqNo<=:transSeqNo order by i.systemAssignId, i.transSeqNo DESC",
                  resultClass = BillingInfo.class)

public class CoTransactionSummary implements java.io.Serializable,
 CdbCoTransactionSummary {


@OneToOne(targetEntity = BillingInfo.class, fetch = FetchType.LAZY)
@org.hibernate.annotations.NotFound(action = org.hibernate.annotations.NotFoundAction.IGNORE)
@Loader(namedQuery = "loadLatestBilling")
public BillingInfo getBillingInfo() {
 return billingInfo;
}

public void setBillingInfo(BillingInfo billingInfo) {
 this.billingInfo= billingInfo;
}

}

In the above example the SystemAssignId would be common between all of the records pertaining to that entity. The transSeqNo would be incremented for each successive transaction and thus the custom loader would look for the highest value associated with the systemAssignId.

Thank you very much for any assistance that you can provide.

+1  A: 

Your named query is wrong - you can't have named parameters in it. You can only have one positional parameter which will be replaced by primary key value (well, there may have to be more than one for composite ids but that doesn't apply in your case). I also don't think it's legal to return more than one value from it; though Hibernate may let it slide.

Now, since the query has to be based off PK (BillingInfo PK) it will not be easy to do what you want via one-to-one mapping (unless, of course, you intend to actually update your CoTransactionSummary every time to point to an appropriate BillingInfo; but in that case you don't need all that custom loading stuff). You'll basically need to rewrite your query to join to BillingInfo table twice - first time to get TransactionSummary PK of BililngInfo record with PK supplied by Hibernate and second time to get the latest BillingInfo record. The whole thing seems rather messy.

Consider using one-to-many mapping instead (you can always make BillingInfo collection private and only expose the appropriate instance via public getBillingInfo() method).

ChssPly76