Hibernate is updating instead of inserting, why? - java

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.

Related

Map hibernate many-to-one with the foreign key in subclass joined table

I'm mapping some entities using Hibernate 3 for my project and simply explained I've got kind of this:
Student entity (tstudent table)
UniversityStudent entity (tuniversitystudent table)
University entity (tuniversity table)
UniversityStudent extends from Student and has its own attributes, like the university itself, which is a foreign key into the tuniversitystudent table. It is also mapped like a subclass into the Student class, using a discriminator field:
<class name="mycompany.Student" table="tstudent" discriminator-value="BASIC">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<discriminator column="type" />
<property name="name" column="name" />
<property name="surname" column="surname" />
<property name="phoneNumber" column="phone_number" />
<subclass discriminator-value="UNIVERSITY"
name="mycompany.UniversityStudent">
<join table="tuniversitystudent">
<key column="id_student" />
<many-to-one name="university" class="mycompany.University">
<column name="id_university" />
</many-to-one>
</join>
</subclass>
</class>
Well, now I want to have a Set collection with the UniversityStudent entities for each University. So I map it like that:
<class name="mycompany.University" table="tuniversity">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<property name="name" column="name" />
<set name="universityStudents" table="tuniversitystudent">
<key>
<column name="id_university" />
</key>
<one-to-many class="mycompany.UniversityStudent" />
</set>
</class>
My problem comes when I want to load a University object, Hibernate complains that id_university doesn't exist in tstudent table. I checked the generated SQL query and it really tries to load it from tstudent.
Unknown column 'student0_.id_university' in 'field list'
It seems that it's recognizing that it is a subclass of the basic Student and tries to join the collection using a field in the parent table, but however the field is actually in the child table, because only university students can have a University assigned.
I tried another workaround which seems to work but it's not valid for me, that's mapping the UniversityStudent as a joined-subclass instead of a subclass with a join inside:
<joined-subclass name="mycompany.UniversityStudent" table="tuniversitystudent">
<key column="id_student" />
<many-to-one name="university" class="mycompany.University">
<column name="id_university" />
</many-to-one>
</joined-subclass>
However, I'm interested in keeping it as a subclass with a discriminator value. Any idea?
I checked out some resources and finally got into this bug: https://hibernate.atlassian.net/browse/HHH-1015, which looks absolutely compatible with your case. Checkout this old question as well, again very similar to your case.
I firstly read the definition of table per sublass given by Hibernate (I know, it is for version 3.3 but I couldn't find the same source for Hibernate 4): joined-subclass seems (to me) to be a custom implementation of subclass using a discriminator provided by Hibernate and that is a good reason to stay away from its usage. However, from what I know, the mappings table per sublass and table per subclass using a discriminator should be equivalent, that's why I believe the bug I pointed you out is really still open.
If you have time and will, you can try to use another JPA provider and check if you still run in the same issue. JPA 2.0 specifications is a thing, provider implementation is another! I recently run into another bug (related to #IdClass) which forced me to try EclipseLink and the configuration which was not working with Hibernate was right with Eclipse Link
Seems you can use Custom SQL (or HQL) for loading. Haven't tried it myself, but looks like, hmm, at least as a last resort, it provides a decent solution.
Define the query in your HBM:
<sql-query name="universityStudents">
<load-collection alias="unistu" role="University.universityStudents"/>
SELECT unistu.*, student.*
FROM tuniversitystudent unistu
JOIN tstudent student
ON unistu.id_student = student.id
WHERE unistu.id_university = :id
</sql-query>
And then use it inside University:
<set name="universityStudents" inverse="true">
<key/>
<one-to-many class="mycompany.UniversityStudent"/>
<loader query-ref="universityStudents"/>
</set>

hibernate deletes list relation when updating other list containing same objects

In my application I have a setup like the following:
class A {
String id;
List<C> list;
}
class B {
String id;
List<C> list;
}
class C {
String id;
}
which is mapped in hibernate like
<class name="A" lazy="false">
<id name="id">
<generator class="org.hibernate.id.UUIDGenerator"/>
</id>
<list name="list" lazy="false" mutable="false">
<key column="listA_id"/>
<list-index column="listA_index"/>
<many-to-many class="C"/>
</list>
</class>
<class name="B" lazy="false">
<id name="id">
<generator class="org.hibernate.id.UUIDGenerator"/>
</id>
<list name="list" lazy="false" mutable="false">
<key column="listB_id"/>
<list-index column="listB_index"/>
<many-to-many class="C"/>
</list>
</class>
<class name="C" lazy="false">
<id name="id">
<generator class="org.hibernate.id.UUIDGenerator"/>
</id>
</class>
All 5 tables (one for each class and 2 for the many-many-relations) are created as expected. The problem appears when I execute some code like the following:
A a = new A();
B b = new B();
C c1 = new C();
C c2 = new C();
C c3 = new C();
<hibernatesession>.save(c1);
<hibernatesession>.save(c2);
<hibernatesession>.save(c3);
a.list.add(c1);
a.list.add(c2);
<hibernatesession>.save(a);
b.list.add(c1);
b.list.add(c3);
<hibernatesession>.save(b);
I followed the resulting SQL in a HSQL-log-file and found everything to be fine up until the last save. Saving b will result in the association between a and c1 being deleted from the many-many-tabel A_list. Fetching the objects a and b anew from the database will result in c1 and c3 being in b.list but only c2 in a.list.
Observations:
* There is no cascading in the game.
* switching the order of saving a/b will result in c1 getting deleted from the list of whoever gets saved first.
* Both lists are mapped with indexes.
* Both lists are mapped with different key/index-column names.
* hibernate deletes exactly those elements common to the lists
Can somebody explain me this behavior of hibernate and knows how to realize a respective mapping correctly?
Thx in advance!!!
Salsolatragus
Try adding a many-to-many mapping to Class C so that it is a bidirectional relationship. Perhaps by being explicit about the "parent" relationships, it will know to keep more than one relationship rather than delete the first one as you have explained.
<class name="C" lazy="false">
<id name="id">
<generator class="org.hibernate.id.UUIDGenerator"/>
</id>
<list name="listA" lazy="false" mutable="false" inverse="true">
<key column="listC_id"/>
<many-to-many class="A"/>
</list>
<list name="listB" lazy="false" mutable="false" inverse="true">
<key column="listC_id"/>
<many-to-many class="B"/>
</list>
</class>
Fixed the bug. Lessons learned: If you have strange correlations between certain Collections in hibernate, check first if you're accidentally using the same Java instance for them... A couple of hours pairDebugging thru hibernate at last led us to the right guess. Tricky thing. Problem solved.
Thx for you hint Brad!

Delete child objects/records in one-to-many relationship using Hibernate

I have a parent-child (one-to-many) relationship between product & packages. one product can have multiple packages. In my 'Edit Product' jsp page I am allowing user to edit product & delete/add/edit any package. Now, on submitting the changes I am doing saveOrUpdate(product) in my controller method. What I am observing is that new packages are getting added in DB but the ones that are deleted by user hence not in the packages Set of Product are not getting deleted from DB. My hiberate configurations snippet is below. Am I missing something here?
<hibernate-mapping>
<class name="author.vo.ProductVO" table="Product">
<id name="ProductID" type="long">
<column name="Product_ID" />
<generator class="increment" />
</id>
<set name="packages" inverse="true" cascade="all" lazy="false">
<key column="Product_ID" not-null="true" on-delete="cascade"/>
<one-to-many class="author.vo.PackageVO" />
</set>
Code for adding product
#Autowired
private HibernateTemplate hibernateTemplate;
this.hibernateTemplate.saveOrUpdate(prod);
You should add "cascade delete orphan" to your xml config, also, why dont you try a simple situation first of all.
is a save working with a parent - child.
is a delete working with a parent - child.

Hibernate - NullPointerException when accessing a child element of one-to-many association

There is a Java SE project with Hibernate ORM. I feel that the problem is trivial, but need some help.
There is a code snippet:
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session s = factory.openSession();
int id = 1;
ExperimentSetResult experimentSetResult = (ExperimentSetResult)s.get(ExperimentSetResult.class, id);
System.out.println("size: " + experimentSetResult.getExperimentResults().size());
System.out.println("id[0]: " + experimentSetResult.getExperimentResults().get(0).getId());
I get a NullPointerException for the last string of code (when accessing the 0-th element of a collection associated with an object loaded recently).
There are the hbm files snippets:
ExperimentResult.hbm.xml:
<hibernate-mapping>
<class name="rmocommon.driverreaction.ExperimentResult" table="experiment_results">
<id name="id" type="int">
<generator class="increment"/>
</id>
<many-to-one class="rmocommon.driverreaction.ExperimentSetResult" name="ExperimentSetResult" column="ExperimentSetResultId" not-null="true" />
</class>
</hibernate-mapping>
ExperimentSetResult.hbm.xml:
<hibernate-mapping>
<class name="rmocommon.driverreaction.ExperimentSetResult" table="experiment_set_results">
<id name="id" type="int">
<generator class="increment"/>
</id>
<list name="ExperimentResults" cascade="all-delete-orphan" inverse="true">
<key column="ExperimentSetResultId" not-null="true"/>
<list-index column="Id"/>
<one-to-many class="rmocommon.driverreaction.ExperimentResult"/>
</list>
</class>
</hibernate-mapping>
What's wrong with mapping or with my source code?
UPDATE:
Here is an output and a stack trace:
Hibernate: select experiment0_.id as id4_2_, experiment0_.StartedDate as StartedD2_4_2_, experiment0_.FinishedDate as Finished3_4_2_, experiment0_.DeviceOutput as DeviceOu4_4_2_, person1_.id as id0_0_, person1_.Login as Login0_0_, person1_.LastName as LastName0_0_, person1_.Patronymic as Patronymic0_0_, person1_.FirstName as FirstName0_0_, person1_.Age as Age0_0_, experiment2_.id as id1_1_, experiment2_.TestMode as TestMode1_1_, experiment2_.TransportType as Transpor3_1_1_, experiment2_.TransportStartSpeed as Transpor4_1_1_, experiment2_.RoadType as RoadType1_1_, experiment2_.RoadLength as RoadLength1_1_, experiment2_.DirectionLeft as Directio7_1_1_, experiment2_.RespondToFirstEffort as RespondT8_1_1_, experiment2_.SoundOnFirstEffort as SoundOnF9_1_1_, experiment2_.ScaleObjects as ScaleOb10_1_1_, experiment2_.ShowTransportSpeed as ShowTra11_1_1_, experiment2_.BarrierXMin as Barrier12_1_1_, experiment2_.BarrierXMax as Barrier13_1_1_, experiment2_.ReactionTime as Reactio14_1_1_, experiment2_.SoundOnBarrierAppearance as SoundOn15_1_1_, experiment2_.AllowedCheatCount as Allowed16_1_1_ from experiment_set_results experiment0_ left outer join persons person1_ on experiment0_.id=person1_.id left outer join experiment_set_settings experiment2_ on experiment0_.id=experiment2_.id where experiment0_.id=?
Hibernate: select experiment0_.ExperimentSetResultId as Experim11_4_1_, experiment0_.id as id1_, experiment0_.Id as Id1_, experiment0_.id as id2_0_, experiment0_.Distance as Distance2_0_, experiment0_.Crash as Crash2_0_, experiment0_.BrakingStarted as BrakingS4_2_0_, experiment0_.BrakingStartedTime as BrakingS5_2_0_, experiment0_.BrakingStartedDistance as BrakingS6_2_0_, experiment0_.BarrierX as BarrierX2_0_, experiment0_.Number as Number2_0_, experiment0_.Time as Time2_0_, experiment0_.Valid as Valid2_0_, experiment0_.ExperimentSetResultId as Experim11_2_0_ from experiment_results experiment0_ where experiment0_.ExperimentSetResultId=?
size: 6
Exception in thread "main" java.lang.NullPointerException
at hibernateTest.HibernateTest.main(HibernateTest.java:45)
Java Result: 1
Might be that your mapping is wrong. Your list-index column definetly should not be ID.
If you really need the ordering, you better create a separate column for that, otherwise you will encounter problems.
Another thing I've noticed. You don't have to specify the inverse on the one-to-many relationship.
It's been a while I've seen hbm.xml files, can you use annotations? They are much easier to understand.

Legacy mapping with hibernate

For my current project I have to map a legacy database using hibernate, but I'm running into some problems.
The database is setup using one 'entity' table, which contains common properties for all domain objects. Properties include (among others) creation date, owner (user), and a primary key which is subsequently used in the tables for the domain objects.
A simple representation of the context is as such:
table entity
- int id
- varchar owner
table account
- int accountid (references entity.id)
table contact
- int contactid (references entity.id)
- int accountid (references account.accountid)
My problem exhibits itself when I try to add a collection mapping to my account mapping, containing all contacts belonging to the account. My attempts boil down to the following:
<hibernate-mapping>
<class name="Contact" table="entity">
<id name="id" column="id">
<generator class="native" />
</id>
<join table="contact">
<key column="contactid"/>
<!-- more stuff -->
</join>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Account" table="entity">
<id name="id" column="id">
<generator class="native" />
</id>
<bag name="contacts" table="contact">
<key column="accountid" />
<one-to-many class="Contact"/>
</bag>
<join table="account">
<key column="accountid"/>
<!-- more stuff -->
</join>
</class>
</hibernate-mapping>
However, when I try to fetch the account I get an SQL error, stating that the entity table does not contain a column called accountid. I see why this is happening: the mapping tries to find the accountid column in the entity table, when I want it to look in the contact table. Am I missing something obvious here, or should I approach this problem from another direction?
This looks to me like you actually need to be mapping an inheritance, using the Table Per Subclass paradigm.
Something like this:
<class name="entity" table="entity">
<id name="id" column="id">
...
</id>
<joined-subclass name="contact" table="contact">
<key column="contactid"/>
</joined-subclass>
<joined-subclass name="account" table="account">
<key column="accountid"/>
</joined-subclass>
</class>
That's approximate by the way - it's described in detail in section 9.1.2 of the Hibernate documentation (just in case you can't find it, it's called "Table per subclass").
Cheers
Rich

Categories