aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate - Exception 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 - Exception" Watch "Hibernate - Exception" New topic
Author

Hibernate - Exception

Maya sekar
Ranch Hand

Joined: Sep 22, 2011
Posts: 70
Hi Everyone,

You know the id tag in hibernate where you usually define your primary key values, well my table does not have a primary value since any of the columns can have duplicate values, but since am using id tag in my code, i cannot seem to run the program without it throwing some sort of exception like the " Hibernate Error: org.hibernate.NonUniqueObjectException".
Is there a way for me to remove the id tag from the hibernate mapping file or is there another way around this problem...


or am I making any sense at all?
Do help!
Thank you
Have a nice day

Maya sekar
Ranch Hand

Joined: Sep 22, 2011
Posts: 70
Hi Everyone,

I just found a better way to explain my problem

Say I have a table with three columns, author book and price

the column "Author" can have duplicate values since one oauthor could have written multiple books and the column "book" can have duplicate values as well since different authors could have written different books under the same name. But the author and book columns together cannot have duplicate values.... get it ?
So in my table i guess i have a composite primary key or something...

So if that is the case, then how do i map these to my hibernate mapping file,
I tired a couple of examples on the net but they are all complex... Can you guys give me something simple...

thanks
Have a nice day
Maya sekar
Ranch Hand

Joined: Sep 22, 2011
Posts: 70
Okay so I mapped the columns as follows,




But i get a org.hibernate.MappingException: composite-id class must implement Serializable

What does that even mean?

Do help!
Thank you very much
Have a nice day
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

As far as i know, we can not create or use a table in hibernate without a primary key (Please correct me if I am wrong). you can add a column named id of type Long & make it a primary key. Also as per the description that you have provided, multiple books of same name doesn't make sense to me....i haven't seen such a scenario in real life so far.

Secondly i don't understand why you are trying composite key here.
sree hareesh
Greenhorn

Joined: Nov 16, 2011
Posts: 11
1) make sure all your pojo classes implemented Serializable interface
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

1) make sure all your pojo classes implemented Serializable interface

I dont agree with sree here, for the current hibernate versions available, we don't have to make entities Serializable.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

Maya sekar wrote:...the column "Author" can have duplicate values since one oauthor could have written multiple books and the column "book" can have duplicate values as well since different authors could have written different books under the same name. But the author and book columns together cannot have duplicate values.... get it ? ...

You seems to have a fundamental issue in your design. I suggest you redesign your entity model (or the object model if you plan to use ORM from the beginning). Your table should have a way to identify each record uniquely, without that you simple cannot retrieve/store(with proper constraints) the data.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Maya sekar
Ranch Hand

Joined: Sep 22, 2011
Posts: 70
Hi,

Thank you all for your response
@vijitha and anuraag : I don't understand why you guys seem to think I have some design issues. Maybe I did not explain myself clearly.

I am trying to use hibernate to fetch data from a database table which does'nt have a primary key defined. However the fact is 3 fields of that table together are unique in the table.

I am not able to figure out how to represent this in the hibernate mapping table for that class. I read that there is a <composite-primarykey..> tag to do this. Tried a few combinations but failed to fetch the entire data from the table.

Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

ok....let me try to explain.. One Name of a book can't be taken for multiple books (Due to legacy issues), also the printed price would be the same for one book. In this case, you can directly have the name of book as your primary Key. I am not understanding the reason of not having this, Please help me understand if you have some point/reason of using composite key.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

By the way International_Standard_Book_Number is a very good way to identify a book.


Please correct my English.
Maya sekar
Ranch Hand

Joined: Sep 22, 2011
Posts: 70
Hi,

Thank you for the response.

The book-author was supposed to be an example, to help make my point. But I guess that was a bad example. Let me try another one. I have a list of products in a table. Each product has a unique id.
Product_table
product_id Product_name
1 shampoo
2 milk
I have another table.. Event table, where actions such as adding to cart (but not actually buying as in paying) and buying are different events.

event_table

Event_id event_name
1 adding to cart
2 purchase

Then I have a third table, reward points where customers are rewarded points based on simply adding to cart or purchasing a product

reward_points_table

product_id event_id reward_points

1 1 25 points
1 2 30 points
2 1 15 points
2 2 20 points

As you can see each column in the third table can have duplicate values, but combination of two keys product_id and event_id cannot be duplicate....

I want to insert values into this third reward_points table using hibernate, in which case i will have to use composite keys... but so far am not able to get it to work....
 
 
subject: Hibernate - Exception