Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Mapping property with view
PostPosted: Fri Apr 04, 2008 2:31 pm 
Newbie

Joined: Fri Apr 04, 2008 2:17 pm
Posts: 15
I have a view created to display a calculated mapping of one key to another key. I want to annotate this in hibernate to make it accessible from my entity. Being a view, I want this property to be read-only.

The specifics of the situation are as follows:

I have an OrderLineItem. Every OrderLineItem has a list of OrderStatuses. This gives a history for each line item's activity, so we can see a line item was created at time X, approved at time Y, and completed at time Z.

Rather than duplicating data on the OrderLineItem table by specifically keying in the latest status (which we care to look at fairly commonly), we have created a view that displays an OrderLineItem PK and OrderLineItemStatus PK.

My OrderLineItem class on the Java side should have some mapping to this property. Most notably, this is very useful for creating dynamic hibernate Criteria objects for sorting/searching. To accomplish this, I've tried something like:

Code:
@ManyToOne
   @JoinTable(name="ORDER_LINE_ITEM_LATEST_STATUS", joinColumns = @JoinColumn(name="LINE_ITEM_ID"), inverseJoinColumns = @JoinColumn( name="STATUS_ID"))
   private OrderLineItemStatus latestStatus;


which reads in correctly. However, it attempts to save this property on update/save and delete it on delete. I don't want it to do that, obviously, because the mapped table is actually a view. This isn't a cascading issue since it thinks it wants to be able to just save/update/delete the row of the view that provides the foreign key rather than dig down and modify the entity being pointed at by the key. Rather it's a matter of saying I want this property to be a 'read-only' table join. So if I delete this entity, then this row in the joined table would be untouched. This SOUNDS like an undesirable behavior because it'd give me an orphan row that wouldn't have any meaning -- except this is a view so the row woud be IMPLICITLY deleted, so in this case it DOES make sense.

Also, I don't want it to be transient because I want to read it in initially.

I've tried creating a setLatestStatus method and placing the annotation on that instead but this did not get called. I saw documentation saying that property and method annotations should not be mixed, further making that an invalid solution.

Any ideas?


Last edited by Gagnonmi on Fri Apr 11, 2008 1:25 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 9:58 am 
Newbie

Joined: Fri Apr 04, 2008 2:17 pm
Posts: 15
I'm still not having any success with this and cannot find help in the annotation documentation http://www.hibernate.org/hib_docs/annot ... ml_single/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 10:49 am 
Newbie

Joined: Fri Apr 04, 2008 2:17 pm
Posts: 15
The following does NOT work
Code:
...
@ManyToOne
@JoinTable(name="ORDER_LINE_ITEM_LATEST_STATUS", joinColumns = @JoinColumn(name="LINE_ITEM_ID"), inverseJoinColumns = @JoinColumn( name="STATUS_ID", updatable=false, insertable=false))
private OrderLineItemStatus latestStatus;
...



The following DOES work
Code:
...
@Entity
@Table(name="ORDER_LINE_ITEMS")
public class OrderLineItem{
@Id
@Column(name="LINE_ITEM_ID")
private Integer lineItemID;
...
@OneToOne
@PrimaryKeyJoinColumn
private OrderLineItemLatestStatus latestStatus;
...

Code:
@Entity
@Table(name="ORDER_LINE_ITEM_LATEST_STATUS")
public class OrderLineItemLatestStatus {
   @Id
   @Column(name="LINE_ITEM_ID")
   private Integer orderLineItem;
   @ManyToOne
   @JoinColumn(name="STATUS_ID", updatable=false, insertable=false)
   private OrderLineItemStatus status;
   
   public OrderLineItemStatus getStatus(){ return status; }
   
   void setStatus(OrderLineItemStatus status){ this.status = status; }
}


Where ORDER_LINE_ITEM_LATEST_STATUS is a view which maps a ORDER_LINE_ITEM key to its most recent ORDER_LINE_ITEM_STATUS key

With this set up, the value from the view is correctly read in when the object is loaded. I can modify the object at runtime (for example when adding a new status, the latest_status object should reflect the new value), and it is correctly NOT saved/updated when comitted. It also correctly does not attempt to remove the view row containing the mapping.

I feel like I've hacked around any possible correct solution with this and I'd still be grateful for any better solution. I'd certainly rather not have a separately annotated object to contain this mapping.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.