Custom OneToOne Hibernate / JPA Association

I have a database that can have different types of relationships based on what data was updated during the last business transaction. An example will be in the application for writing a policy. The main table contains general policy level information, and there are related tables containing information such as agent information and information about insured persons. When the first transaction is issued, all tables are filled, however, subsequent transactions only store the updated tables in the database. Now I am trying to create classes that implement JPA that define relationships between tables as one-to-one. Then I would like to implement custom loaders for each of the relations to ensure that every time I get the policy object,I can get the most recent version of the relevant tables. Does anyone know how to implement this logic? I tried to use@NamedNativeQuery

and use HQL, but in both cases it tells me that the column name I am specifying does not exist.

Any help would be greatly appreciated.

The code is 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 will be propagated to all records related to this object. The TransSeqNo will increment for each subsequent transaction, and thus the custom loader will look for the largest value associated with systemAssignId.

Thanks so much for any help you can provide.

+2


source to share


1 answer


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

Now, since the request must be PK (BillingInfo PK) based, it won't be easy to do what you want with a one-to-one mapping (unless of course you intend to actually update CoTransactionSummary

yours to point to the appropriate one each time BillingInfo

, but this in case you don't need all those custom downloads). Basically you will need to rewrite your query to join the table BillingInfo

twice - the first time to get the TransactionSummary

PK from the BililngInfo

record with the PK provided by Hibernate, and the second time to get the last record BillingInfo

. It all seems pretty messy.



Consider using a mapping instead one-to-many

(you can always make the BillingInfo

collection private and only expose the corresponding instance using a public method getBillingInfo()

).

+2


source







All Articles