Hibernate Books

All times are UTC - 5 hours [ DST ]

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Delete Element of Collection of Superclass with Hibernate
PostPosted: Mon Apr 10, 2017 6:58 pm 

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
I mapped some classes of an hierachy with strategy "TABLE PER CONCRETE CLASS", which also means my superclass Element doens't have its own table. Subclasses of Element are ElementA and ElementB, those have their own tables.

And then in an another entity, i mapped a ONE-TO-MANY Collection like this :
    @OneToMany (fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Element> elements = new ArrayList<>();

The problem is, i failed to either delete or adding elements into the List. When i tried to delete by using session.saveorUpdate(), it says
java.lang.IllegalArgumentException: Removing a detached instance

And when i tried to add a new Element to the list :

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table Element doesnt exist.

Which is understandable, because there is no such table.

For clarity, my superclass is mapped like this.

    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    public abstract class Element { ...

Getting elements from the database work completly okay though.
What did i do wrong here ? Thanks for any help.

 Post subject: Re: Delete Element of Collection of Superclass - Hibernate
PostPosted: Tue Apr 11, 2017 1:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1105
As I explained in my book, High-Performance Java Persistence, TABLE_PER_CLASSis not very efficient from a SQL statement perspective.

In your example, I guess it's because the Element class is abstract that you get some of your issues, so you should make the changes like this:

2. Make the Element non-abstract
3. Remove fetch = FetchType.EAGER from the @OneToMany collection since it's going to cause performance problems
4. Use a ManyToOne association in Element instead of a unidirectional @oneToMany as explained in this article and turn the association into a bidirectional one
5. Once the association is bidirectional, make sure you synchronize both ends of the association as explained in this article

Follow all these steps, and you should be fine.

If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.