If the categories already exist, then you should not create new instances. You should get them from the database, using the Hibernate session.
Up vote 0 down vote favorite share g+ share fb share tw.
I have two entities in hibernate (MySQL database storage) which are related many to many. These are "Project" and "Category". Project Category I have a join table named "project_category" to join these entities stored in "project" and "category" tables.
The join table stores only primary keys from these tables. These keys are "project_id" and "category_id". I have fixed number of Categories.
Category table is already populated with possible list of categories for the projects. When new project is created, it belongs to one or more categories and that is stored in In my mapping for "Project" class I have specified many to many navigation from Project to Category using "project_category" table which is as follows: This is what I want to achieve.. I want to save the Project and associated categories (here I don't want to create any new category just associate the category_id with my project_id). Categories already exist and I want to use them whenever new Project is saved.
In code, I have created a collection of desired categories (out of many categories that I have in Category table) and added to Project instance and trying to save the Project using hibernate. Now expecting project_category to contain association of project and categories. I am observing that while saving Project, hibernate trying to Category (which is not what I want because I already have category created - I want to just associate it with my new project).
What kind of mapping need to achieve above goal? I read this good article but here author is creating both entities rather than using already created one. http://technicalmumbojumbo.wordpress.com/2007/09/25/investigating-hibernate-associations-many-to-many/ hibernate hibernate-mapping link|improve this question edited Feb 17 at 7:55JB Nizet69.6k31545 asked Feb 17 at 7:46Anil1.
If the categories already exist, then you should not create new instances. You should get them from the database, using the Hibernate session. If you have the IDs of the categories you need to associate with a new project, and you don't want to issue a select query to get thosee categories from the database (because there is no way these categories aren't in database anymore), then simply use the Session.load() method: Set categoryIds = ...; for (Long categoryId : categoryIds) { Category category = (Category) session.
Load(Category. Class, categoryId); newProduct. Add(category); }.
Thanks for the answer. Actually the problem was with .persist() call. I changed it to .save() and it worked.
Don't know why it was trying to insert in to Categories table. – Anil Feb 23 at 19:44 you have a cascade=persist in your mapping, but no cascade=save. – JB Nizet Feb 23 at 19:46.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.