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?
Have a nice day
Joined: Sep 22, 2011
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...
Have a nice day
Joined: Sep 22, 2011
Okay so I mapped the columns as follows,
But i get a org.hibernate.MappingException: composite-id class must implement Serializable
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.
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.
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.
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.
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.
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.
1 adding to cart
Then I have a third table, reward points where customers are rewarded points based on simply adding to cart or purchasing a product