Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9312879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:44:26+00:00 2026-06-19T01:44:26+00:00

I’m using spring-data JPA with hibernate. I’m having extreme difficulty of getting my inheritance

  • 0

I’m using spring-data JPA with hibernate. I’m having extreme difficulty of getting my inheritance and relationship mappings to work correctly.

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name="compound")
@DiscriminatorColumn(name="compound_type")
@DiscriminatorOptions(force=true)
public abstract class Compound<T extends Containable> {

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.compound",
        cascade = CascadeType.ALL, orphanRemoval = true)
    @LazyCollection(LazyCollectionOption.FALSE)     
    private List<CompoundComposition> compositions = new ArrayList<>(); 

    @OneToMany(fetch = FetchType.EAGER, mappedBy="compound",
        targetEntity=Containable.class, cascade = CascadeType.ALL) 
    @LazyCollection(LazyCollectionOption.FALSE)     
    private Set<T> containables = new HashSet<T>();

}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name="containable")
@DiscriminatorColumn(name="containable_type")
@DiscriminatorOptions(force=true)
public abstract class Containable<T extends Compound> {     

    @ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private T compound;
}

The idea is that a certain implementation of AbstractCompound can only be associated with one specific implementation of Containable (and vice-versa). This leads to following implementaions:

@Entity 
@DiscriminatorValue("TestCompound") 
public class TestCompound extends AbstractCompound<TestContainable> {
}

@Entity 
@DiscriminatorValue("RegistrationCompound")
public class RegistrationCompound extends AbstractCompound<Batch> {

    @Column(name = "reg_number", unique = true)
    private String regNumber;
}

@Entity 
@DiscriminatorValue("TestContainable")  
public class TestContainable extends Containable<TestCompound> {
}

@Entity 
@DiscriminatorValue("Batch")    
public class Batch extends Containable<RegistrationCompound>{

    @Column(name = "batch_number")
    private Integer batchNumber;
}

I’ve played around with all the inheritance strategies and for Compound-hierarchy single-table is the only one that at least partially works. In case of JOINED or table _per_class hibernate creates inconsistent and wrong!!! foreign keys, namely from test_containable to registration_compound (But not from Batch to test_compound, here it correctly maps to registration_compound only).

On Containable side it does not seem to matter what strategy I use.

Now to the actual issue in my Tests. The specific test class. has 3 tests. All doing a specific search for a “TestCompound” instance. The thing is the first executed of these 3 test cases always passed, the other 2 always fail. The order at which the are run seems to be random (JUnit + @RunWith(SpringJUnit4ClassRunner.class)). This means that any of the tests pass, if it is the first one to run.

The tests that fail throw following exception:

org.hibernate.WrongClassException: Object with id: 1000 was not of the specified
    subclass: RegistrationCompound (loaded object was of wrong class class TestCompound)

In cases of the first test, hibernate issues following correct select for fetching the Containables

Hibernate: select containabl0_.compound_id as compound8_1_1_, containabl0_.id as id0_1_, 
containabl0_.id as id0_0_, containabl0_.created as created0_0_, 
containabl0_.created_by as created4_0_0_, containabl0_.last_modified as last5_0_0_, 
containabl0_.last_modified_by as last6_0_0_, containabl0_.compound_id as compound8_0_0_, 
containabl0_.batch_number as batch7_0_0_, containabl0_.containable_type as containa1_0_0_ 
from containable containabl0_ where  containabl0_.containable_type in ('Batch', 'TestContainable') 
and containabl0_.compound_id=?

and the List<CompoundComposition> compositions is selected in another select statement. So they are a total of 3 statements: Get compound, get containables, get compositions.

For the second and 3rd test, the SQL for fetching the containables is merged with the on for fetching compositions and it is build in a way so it is trying to select a RegistrationCompound instead of a TestCompound, as example it contains

registrati1_.reg_number as reg10_1_0_, 

and reg_number is a property of RegistrationCompound only. In both cases the first select statement that selects the actual compound correctly contains the following in the where clause:

testcompou0_.compound_type='TestCompound'

So this is very confusing. Why does it depend on the order the test are run? Why in the heck does it try to select a RegistrationCompound?

Here is the simplest test of the 3 tests:

@Test
@Transactional
public void testFindByCompositionPkStructureId() {
    System.out.println("findByCompositionPkStructureId");

    Long structureId = 1000L;

    TestCompound compound = new TestCompound();
    compound.setId(1000L);
    compound.setCas("9999-99-9");
    compound.setCompoundName("Test Compound");

    List<TestCompound> result = 
        testCompoundRepository.findByCompositionsPkStructureId(structureId);
    assertEquals(compound, result.get(0));
}

If this test is run as the second or third I get the wrong class exception!!! Does anyone have any idea what the heck is going on here? Solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:44:27+00:00Added an answer on June 19, 2026 at 1:44 am

    The issue was one of the mappings:

    @Entity
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    @Table(name="containable")
    @DiscriminatorColumn(name="containable_type")
    @DiscriminatorOptions(force=true)
    public abstract class Containable<T extends Compound> {     
    
        @ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
        private T compound;
    }
    

    This mapping is missing the target entity. Correct is

    @ManyToOne(optional=true, fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity = Compound.class)
    

    For some reason hibernate just assumed the target is RegistrationCompound instead of throwing an exception. Pretty annoying because else it would have been easy to find the issue. But like this it almost drove me crazy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I am using jsonparser to parse data and images obtained from json response. When
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.