Is it possible to have an unidirectional many-to-many mapping in hibernate, so that duplicates of the "child" does not get saved? I'm using XML mapping and not the annotations, because that's what I happened to start with.
I have only found this one as a potential solution, but I'd like to know if hibernate can be configured without coding: Hibernate cascade many to many creates duplicates in child reference
In my case I unmarshall XML with jaxb to get java objects, and I don't need bidirectional mapping.
I have a class Article(Item), that has a Set of Category, and the Category class has no reference back to Article. What I'd like to do is save the article object, and only insert categories into the Category table if they do no yet exist. The way categories are linked to Articles are by a linking table.
The reason I want to get it to work without coding is that I wanted to learn something new (hibernate). Perhaps I have the wrong perception of Hibernate, I thought it can figure out what it need to do if I configure it right. So far I've spent almost one day learning hibernate and it does not feel very productive considering I could just have designed the db tables myself and done the code for inserting/updating correctly in an hour.
Here is an example that is similar to mine, where they save 2 students, both having the same courses. Still, the courses are saved only once in the course table: http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html
Here's one more example, but it is bidirectional and I can't make the inverse mapping in the same way since the Category does not have a reference back to Article: http://viralpatel.net/blogs/hibernate-many-to-many-xml-mapping-example/ (Viral Patel has really good tutorials by the way=)
Article has properties:
String Title;
Set<Category> categories;
Category has property:
String category;
In the Item(Article) mapping I have this so far:
<set name="categories" cascade="save-update" table="ItemCategory">
<key column="itemID"/>
<many-to-many column="categoryID" class="Category"/>
</set>
It almost works as I want it to, only it does save same categories multiple times in the Category table and that seems unnecessary.
I mostly want to use Hibernate for just avoiding designing the tables and easily dump my RSS objects into a structured format (like a nosql database).
Update: The duplicates comes from when other Items (Articles) are added that have the same categories that already exist in the Category table. E.g. the first Item has the categories Economy and Politics the second article has Economy
When the second article (Item) is saved I'd like hibernate to add an entry in the joining table to the existing Economy row and not create a new one.
I already tried making the category unique but then I get sql errors from hibernate that the index already contains the key.
Update 2: I changed the Item class to have a set of strings instead of categories. The mapping now looks like:
<set name="categories" table="Category">
<key column="itemID"/>
<element column="category" type="string"/>
</set>
This removes the joining table I desired for avoiding data duplication. Now categories or tags appear in the category table as many times they have occurred in all Items (articles).
The whole idea was to reduce the data in the database when storing 1000s of items. The same categories or tags are mostly used, so if a joining table could be used in such way that the Category table would only contain unique categories, then a lot of redundancy could be removed.
Update 3: My problem seems similar to this one: Using saveOrUpdate() in Hibernate creates new records instead of updating existing ones
Update 4: I switched back to having a set of categories in the Item class, to test if it works by having Category.category as key. The mapping for item:
<set name="categories" cascade="save-update" table="ItemCategory">
<key column="itemID"/>
<many-to-many column="category" class="Category"/>
</set>
The mapping for Category:
<id column="category" name="category" unsaved-value="0">
<generator class="assigned"/>
</id>
The result is I get the exception: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session