hibernate many to many relations cascade - java

Newbie to hibernate i have two tables A and B that have many to many relations defined by a table AB(A_ID and B_ID) with foreign key reference to A.A_ID and B.B_ID and cascade on delete and update defined.
I have mapped
a.hbm.xml has
<set name="bSet" table="AB" inverse="true" lazy="false" fetch="select" cascade="all">
<key>
<column name="A_ID" not-null="true" />
</key>
<many-to-many class="objectB" >
<column name="B_ID" not-null="true" />
</many-to-many>
</set>
b.hbm.xml has
<set name="aSet" table="AB" inverse="false" lazy="false" fetch="select" cascade="all">
<key>
<column name="B_ID" not-null="true" />
</key>
<many-to-many class="objectA">
<column name="A_ID" not-null="true" />
</many-to-many>
</set>
//ObjectA.java has
private Set<ObjectB> bSet = new HashSet<objectB>(0);
//ObjectB.java has
private Set<ObjectA> aSet = new HashSet<objectA>(0);
From the front end sends the A object as a json with set of B's the table A is getting updated correctly while the AB is untouched.
Can someone point out where am I going wrong?
Here is the JSON
{
"a_field1": "value1",
"a_field2": "value2",
"aId": 1,
"bSet": [
{
"bId": 100
},
{
"bId": 200
}
],
"a_field3": "value3"
}
initially the db is set up with 3 records in AB table
(1,100)
(1,200)
(1,300)
The final results in the db should have been
(1,100)
(1,200)
the last row (1,300) should have been deleted.
Any help is greatly appreciated.
Shah

My best guess (you don't provide any example of the server code that handles the request) is that you're only updating one side of the bidirectional association. In other words you're just deserializing that A instance and doing a merge. If you get a new A you still need to merge the A instance but you also need to load up all the B's that A no longer references and remove the A instance from their list and also look up all the B's that are newly referenced by A and add A to their list. It's one of the hazards of a bidirectional relationship in code.

Related

Cannot delete child in one-to-many relationship

When I put inverse=true into set, nothing gets deleted. When I don't, and I remove MealIngredient from set, then Hibernate tries to set null, it fails and exception is thrown:
[SQLITE_CONSTRAINT] Abort due to constraint violation (MealIngredients.mealId may not be NULL)
Here are XML mappings:
<class name="restaurant.meal.Meal" table="Meals">
<id name="id" type="integer">
<column name="id" not-null="true" unique="true"/>
<generator class="increment"/>
</id>
<!-- some other, simple properties -->
<set name="ingredientsSet" cascade="all" lazy="false">
<key>
<column name="mealId" not-null="true" />
</key>
<one-to-many class="restaurant.meal.MealIngredient" />
</set>
</class>
<class name="restaurant.meal.MealIngredient" table="MealIngredients">
<composite-id name="id" class="restaurant.meal.MealIngredient$Id">
<key-property name="ingredientId" />
<key-property name="mealId" />
</composite-id>
<many-to-one name="ingredient" class="restaurant.storage.Ingredient" insert="false" update="false" lazy="false">
<column name="ingredientId" not-null="true" />
</many-to-one>
<many-to-one name="meal" class="restaurant.meal.Meal" insert="false" update="false" lazy="false">
<column name="mealId" not-null="true" />
</many-to-one>
<!-- other properties -->
</class>
Yes, the relationship between Meal and Ingredient is many-to-many with join table MealIngredient (and yes, I have to map MealIngredient as well, because of additional columns in that table).
This question did not help me, neither did this.
Edit:
Only inserting works with current mapping, update just generates another row in MealIngredient table.
Edit 2: hashCode and equals implementations:
MealIngredient$Id: (uses Apache commons-lang EqualsBuilder and HashCodeBuilder)
#Override
public boolean equals(Object o) {
if(!(o instanceof Id))
return false;
Id other = (Id) o;
return new EqualsBuilder()
.append(this.getMealId(), other.getMealId())
.append(this.getIngredientId(), other.getIngredientId())
.isEquals();
}
#Override
public int hashCode() {
return new HashCodeBuilder()
.append(this.getMealId())
.append(this.getIngredientId())
.hashCode();
}
MealIngredient:
#Override
public boolean equals(Object o)
{
if(!(o instanceof MealIngredient))
return false;
MealIngredient other = (MealIngredient) o;
return this.getId().equals(other.getId());
}
#Override
public int hashCode()
{
return this.getId().hashCode();
}
I checked log and although I don't know what Hibernate does under the hood, but it does make the insert into MealIngredient:
15:42:53,122 TRACE IntegerType:172 - returning '5' as column: quantity3_
Hibernate:
insert
into
MealIngredients
(quantity, ingredientId, mealId)
values
(?, ?, ?)
15:42:53,131 TRACE IntegerType:133 - binding '16' to parameter: 1
15:42:53,131 TRACE IntegerType:133 - binding '5' to parameter: 2
15:42:53,131 TRACE IntegerType:133 - binding '1' to parameter: 3
And when Iā€‚remove MealIngredient from Meal.ingredientsSet, Hibernate makes update and tries to set mealId to null:
Hibernate:
update
MealIngredients
set
quantity=?
where
ingredientId=?
and mealId=?
15:48:57,529 TRACE IntegerType:126 - binding null to parameter: 1
15:48:57,529 TRACE IntegerType:133 - binding '1' to parameter: 2
15:48:57,531 TRACE IntegerType:133 - binding '1' to parameter: 3
15:48:57,535 WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: null
15:48:57,535 ERROR JDBCExceptionReporter:78 - [SQLITE_CONSTRAINT] Abort due to constraint violation (MealIngredients.quantity may not be NULL)
I believe the explanation you're looking for is here. Well, sort of. Don't read his explanation, it confuses me. His examples are excellent though.
So, anyways, I think you want to do one of the following:
inverse=false and remove the mealIngredient from your ingredients
collection and then save the Meal
inverse=true and have to null the meal instance variable in MealIngredient and save the MealIngredient
EDIT: The issue with inserts instead of updates is probably due to the fact that you have not over-ridden hashcode and equals. If you're using Eclipse, I believe it can do it for you, but you must tell it to use both properties of your composite key when it auto generates the methods. Per Hibernate documentation chapter 5:
The persistent class must override equals() and hashCode() to
implement composite identifier equality. It must also implement
Serializable.
Unfortunately, it seems that Hibernate does not work well with composite primary keys. I had to add extra ID column into many-to-many join tables (like my MealIngredient) and work with that.
After I use extra ID as primary key, inserting/updating/deleting works as expected (even with cascade set to delete-orphan, cascade deleting works!).
I provide final mappings for entities Meal and MealIngredient, for future reference. I hope this will help others, when they stumble upon many-to-many relationships with additional properties/columns in join table.
<class name="restaurant.meal.Meal" table="Meals">
<id name="id" type="integer">
<column name="id" not-null="true" unique="true"/>
<generator class="increment"/>
</id>
<!-- additional properties -->
<set name="ingredientsSet" table="MealIngredients" cascade="all-delete-orphan" lazy="false" inverse="true">
<key update="true">
<column name="mealId" not-null="true" />
</key>
<one-to-many class="restaurant.meal.MealIngredient" />
</set>
</class>
<class name="restaurant.meal.MealIngredient" table="MealIngredients">
<id name="id" type="integer">
<column name="id" not-null="true" unique="true"/>
<generator class="increment"/>
</id>
<many-to-one name="ingredient" column="ingredientId" not-null="true" class="restaurant.storage.Ingredient" lazy="false" />
<many-to-one name="meal" column="mealId" not-null="true" class="restaurant.meal.Meal" lazy="false" />
<!-- additional properties -->
</class>

Hibernate Adding where clause on one-to-many mapping

I have this set on my .hbm file.
<set name="subTopicsTb" table="subtopics_tb" inverse="true" lazy="false" fetch="select">
<key>
<column name="topic_id" />
</key>
<one-to-many class="com.topics.model.SubTopics" />
</set>
now, the default is that, hibernate get's all subtopics where the topic_id is the id.
i want to filter the subtopics. adding something like where subTopics.date is not null thanks
This actually works, being date the column name in your DB. where attribute in the set appends raw sql to your query, so you have to specify as it is in your database and NOT HQL:
<set name="subTopicsTb" table="subtopics_tb" inverse="true" lazy="false"
fetch="select" where="date is not null">
<key>
<column name="topic_id" />
</key>
<one-to-many class="com.topics.model.SubTopics" />
</set>
Add a where clause? I don't know how you set that in an XML config .. but you can check out the annotation version here.
I found something at stackoverflow on how to add the where to your XML.
Are you using HQL to retrieve your SubTopics? If so, you can include the filter in your selection. For example:
String query = "FROM SubTopic subtopic WHERE subtopic.date != null"

Hibernate Mapping of Join Table with Meta Data

I'm trying to figure out how to map the relationship between two tables through a join table that has some meta data in it. In short, the three tables represent the page of a form, and each page can contain any number of elements (questions.) For some reason, the original developer decided that elements could be used on multiple forms. This means that the weight column, used to order the elements on the page, is in the join table.
How the heck do I map this in XML? (Annotations aren't an option.)
For the join table, I guess it's like this:
<class name="com.foo.bar.model.db.ApplicationPageElements"
table="APPLICATION_PAGE_ELEMENTS">
<composite-id name="id" class="com.foo.bar.model.db.ApplicationPageElementsKey">
<key-property name="pageId" column="page_id"/>
<key-property name="elementId" column="element_id"/>
</composite-id>
<property name="weight" type="java.lang.Long">
<column name="WEIGHT" precision="0" />
</property>
</class>
My instincts have me wanting to do something like this from the ApplicationPage side of things:
<set name="applicationElements" table="applicationPageElement">
<key column="page_id"/>
<many-to-many column="element_id" unique="true"
class="com.foo.bar.model.db.ApplicationElements" />
</set>
And that's where I get all slack-jawed, stare at the screen, and sob.
We're using .hbm.xml files to map our database. We also made the decision to not change our database.
Any ideas on how to map this in XML?
Instead of thinking of the relationship between application_page and application_element as many to many, think of it as a one to many relationship from application_page to ApplicationPageElements and a one to many relationship from application_element to ApplicationPageElements.
In your application_page xml mapping add this:
<set name="applicationElements" inverse="true">
<key column="page_id"/>
<one-to-many class="ApplicationPageElements"/>
</set>
page_id forms a part of the primary key of the join table. So, mark the collection as inverse.
Your mapping for the join table is correct. But, with the above change current mapping of your join table you can navigate from application_page to ApplicationPageElements. To navigate from application_page to application_element (via ApplicationPageElements) add a many to one relationship in join table mapping.
<class name="com.foo.bar.model.db.ApplicationPageElements"
table="APPLICATION_PAGE_ELEMENTS">
<composite-id name="id" class="com.foo.bar.model.db.ApplicationPageElementsKey">
<key-property name="pageId" column="page_id"/>
<key-property name="elementId" column="element_id"/>
</composite-id>
<property name="weight" type="java.lang.Long">
<column name="WEIGHT" precision="0" />
</property>
<many-to-one name="elements" class="ApplicationElements"
column="element_id" not-null="true" insert="false" update="false"/>
<many-to-one name="page" class="ApplicationPage"
column="page_id" not-null="true" insert="false" update="false"/>
</class>
Note that in the above many-to-one mapping, insert and update attributes are set to false. This is necessary because the columns are mapped twice, once in the composite key (which is responsible for insertion of the values) and again for the many-to-one associations.
The above use case is mentioned in detail in the book: Java Persistence with Hibernate.

Hibernate is updating instead of inserting, why?

I'm starting my adventure with Hibernate, so please be patient :)
I want to make mapping for two tables, for example A and B. The relation beetwen A and B is one-to-many.
I wrote this hbm.xml file:
<hibernate-mapping package="something">
<class name="A" table="A">
<id name="id" type="int" column="ID">
<generator class="native" />
</id>
<set name="setInA" sort="natural" cascade="all" lazy="false">
<key column="ANOTHER_ID"/>
<one-to-many class="B" />
</set>
</class>
<class name="B" table="B">
<id name="anotherId" type="int" column="ANOTHER_ID">
<generator class="native" />
</id>
</class>
</hibernate-mapping>
Of course I made also POJO classes A and B.
And now, when I try to do:
A a = new A();
Set<B> set = new TreeSet<B>();
set.add(new B());
a.setSetInA(set);
session.save(a);
Hibernate inserts new row to table A, but (what is the worst) is not inserting new row to B table, but only makes SQL Update on not existing row in B.
Can tell me anyone why it is happening? What I made wrong?
You should either persist B's objects firstly, or use Cascade option.
You can use Cascade without using annotations:
<set name="setInA" sort="natural" cascade="all" lazy="false" cascade="all">
<key column="ANOTHER_ID"/>
<one-to-many class="B" />
</set>
This will ensure that collection of B instances is inserted when you insert A.
Found this question while searching for causes of the same symptoms in my system. cascade="all" did not help.
In my case I solved this by adding a mapping to the list element, in this example class B.
Please note that the enclosing class (A in this example) also was versioned. Hibernate might require that versioning (used for optimistic locking) must be enabled for all nested elements. I haven't found any documentation to support this theory.

Hibernate Cross Reference many-to-many Parent/Child Mapping Of Same Class

I'm having a major problem getting a parent/child relationship working for a hierarchy of a single class. Basically I have to represent a server tree like thus:
Server A
Server B
Server C
Server D
Server E
Server C
Server D
Server F
Server G
Note that the children of Servers B & E are the same. My original mapping was something like this, which was fine until I needed to have the objects for server C & D being the same instance, so having a single column for PARENT_ID got filled by the last relationship and only one of servers B or E would show the children:
<hibernate-mapping ...>
<class name="Server" ...>
...
<set name="children" cascade="all-delete-orphan" lazy="false">
<key column="PARENT_ID" />
<one-to-many class="Server" />
</set>
</class>
</hibernate-mapping>
I know I need to do some sort of cross reference table to map the fact that a server can have multiple parents, but all the examples I've found on-line contain a separate parent and child class.
Can anyone tell me how to do a cross reference parent/child mapping for the same class...? I.e. something like:
<hibernate-mapping ...>
<class name="Server" ...>
...
<set name="children" cascade="all-delete-orphan" lazy="false">
<key>
<column name="PARENT_ID" />
<column name="CHILD_ID" />
</key>
<many-to-many class="Server">
<column name="???" />
<formula>???</formula>
</many-to-many>
</set>
</class>
</hibernate-mapping>
Thanks,
Bob.
In your many-to-many mapping, set the column name to be CHILD_ID.
<many-to-many class="Server">
<column name="CHILD_ID" />
</many-to-many>
This will cause the relationship to view the child id as the id representing itself. While the one-to-many relationship will use the parent_id as the id representing itself. Should work, I haven't ran it, but I have done a similar thing before.
Doing the following:
...
<set name="children" table="SERVER_XREF" cascade="all-delete-orphan" lazy="false">
<key column="PARENT_ID" />
<many-to-many class="Server" column="CHILD_ID" />
</set>
...
appears to have resulted in the server hierarchy I was after being returned.
Cheers,

Categories