wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate object mapping... design issue? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate object mapping... design issue?" Watch "Hibernate object mapping... design issue?" New topic
Author

Hibernate object mapping... design issue?

Marc Cracco
Ranch Hand

Joined: Mar 09, 2010
Posts: 77
    
    1
So this is my first post, as a little background I am very new to this. I haven't gone to school for this but have been working hard on java for a while now. I just passed my Sun Certified Java Programmer certification and try to learn more everyday.

Now I think my problem might be a design issue and hope someone can help me with it.

Project: I am writing a program for a friend and using it as a learning experience. The overall scope of the project is as follow.
Writing a web based application for a dog grooming shop. It keeps track of pets which are tied to owners (customers)
Then for the billable items there are BillableItem objects which belong to Categories.
From invoicing a user would choose an owner, then a pet.
Then a category and a billable item.
All these objects are mapped to mysql with hibernate. They all have individual id's.
All these objects are served to the client (browser) via servlets. All communication to and from the server/client are done via post using xml.

In the invoicing stage I want the user to be able to do a one time edit of an item price, like an override. This should not change the price of the item permanently but for
a one time occurrence only. When the invoice is submitted the invoice should map to a table and each invoice item should map to another table where the
invoice id, item id and cost for that specific invoice is. That way when I load up an invoice from history, it will load up the price of that occurrence and not the price of the
item now.

My invoice object has all it's variables... ie total, tax, paymentType...... and a List<BillableItems> problem is when I send back the info from the client to the servlet and
try to create a billableItem with the ID of the original and the new price and try to save my invoice hibernate freaks (to be expected) because there already is a billableItem
with that ID associated with the session....



I'm sure this is just because of bad design and that there is a clean way around this. The problem is that like I said I'm new to this including hibernate and servlet and jsp and...
well you get the point.

Any help, advice will be gladly accepted and I apologize in advance if I ask what might seem like obvious questions...

Thanks
Marc


SCJP 6 [ My stuff ]
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
Hi Marc,

Can you tell us the tables structure and how they are joined and also your mapping files...


Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
Marc Cracco
Ranch Hand

Joined: Mar 09, 2010
Posts: 77
    
    1
Ok so here is my table structure: http://pastie.org/861734
Here is Invoice.hbm.xml http://pastie.org/861757
here is the Invoice.java http://pastie.org/861754
Here is the Billableitem.hbm.xml http://pastie.org/861746

Billableitems get sent to client via servlet using XML
They get added to the invoice as requested by the user
Then when the user is happy with the invoice they submit to a servlet via XML. This is all stateless so the
server has no idea what items are there. The server creates an invoice, adds the billableitems in a set
when I try to save the invoice though I get:

SEVERE: Servlet.service() for servlet CreateInvoiceServlet threw exception
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.marc2912.tailWagger.billableItem.Billableitem#9]
.............
Marc Cracco
Ranch Hand

Joined: Mar 09, 2010
Posts: 77
    
    1
So thinking about this and I came up with this. I shouldn't store the billableitems on an invoice as billableitems. Reason being is that they are not a billableitem but an edited version of one.
I'm wondering if I should somehow just have a list in my invoice that would just keep the billableitem id and the cost but not as an object but two variables. Then when I would load my invoice
from memory i could load the billableitem with the id and then use my setter to set the price on load if different. Since the object itself would not be saved but just those variables hibernate
shouldn't cry foul... maybe?
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
Marc Cracco wrote:So thinking about this and I came up with this. I shouldn't store the billableitems on an invoice as billableitems. Reason being is that they are not a billableitem but an edited version of one.
I'm wondering if I should somehow just have a list in my invoice that would just keep the billableitem id and the cost but not as an object but two variables. Then when I would load my invoice
from memory i could load the billableitem with the id and then use my setter to set the price on load if different. Since the object itself would not be saved but just those variables hibernate
shouldn't cry foul... maybe?



Sounds good to me...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate object mapping... design issue?
 
Similar Threads
How to lazily load references when the object is detached from session ?
How to pass "id" to any method by means of <input type="checkbox"> and submit button?
Hibernate .hbm.xml - Referential Integrity constraint
problems with hibernate: one-to-many and arrays
what technology & architecture to choose for a display board