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

  • Home
  • SEARCH
  • 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 7688765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:59:56+00:00 2026-05-31T19:59:56+00:00

I am trying to work out if it is possible get JPA to persist

  • 0

I am trying to work out if it is possible get JPA to persist an abstract collection with concrete implementations.

So far my code looks like this:

@Entity
public class Report extends Model {

    @OneToMany(mappedBy = "report",fetch=FetchType.EAGER)
    public Set<Item> items;
}

@MappedSuperclass
public abstract class OpsItem extends Model {

    @ManyToOne
    public RetailOpsBranch report;
}


@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class AItem extends OpsItem {
...
}

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class BItem extends OpsItem {
...
}

But I keep stumbling on the mapping error below and I don’t really know if this is feasible?

JPA error
A JPA error occurred (Unable to build EntityManagerFactory): Use of @OneToMany or
@ManyToMany targeting an unmapped class: models.Report.items[models.OpsItem]

UPDATE

I don’t think the problem is with the abstract class but the the @MappedSuperClass annotation.
It looks like jpa does not like to map the one to many relationship with a @MappedSuperClass.
If I change the abstract class into a concrete class I have the same error.

If I then change to @Entity annotation this seem to work with both the abstract and concrete class.

This seems a bit strange to map an abstract class with @Entity.
I’m I missing anything?

SOLUTION

Managed to figure it out with the help from rhinds.
Two points to note:

1) the abstract class needs to be annotated with @Entity and inheritance strategy of table per class for the subclasses to have their own table.

2) Identity Id generation will not work in this scenario and I had to use Table generation type.

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class OpsItem extends GenericModel {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public Long id;

    public String          branchCode;

    @ManyToOne
    public Report report;
}

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class AItem extends OpsItem {
...
}
  • 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-05-31T19:59:57+00:00Added an answer on May 31, 2026 at 7:59 pm

    Yep, it is possible. You should only have the MappedSuperClass on the top abstract class (that isnt going to be persisted in itself) and add Entity annotation on the implementation classes.

    Try changing it to something like this:

    @MappedSuperclass
    public abstract class OpsItem extends Model {
    
        @ManyToOne
        public RetailOpsBranch report;
    }
    
    @Entity
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    public class AItem extends OpsItem {
    ...
    }
    
    @Entity
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    public class BItem extends OpsItem {
    ...
    }
    

    Check this section of the hibernate docs for details on its use: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e1168


    UPDATE

    Sorry, completely missed the table per class bit. Hibernate does not support mapping of abstract objects for table per class (can only map List if all implementations are within a single SQL table, and the TABLE_PER_CLASS uses the “table per concrete class” strategy.

    Details of limitations and the strategies here: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#inheritance-limitations

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

Sidebar

Related Questions

Im trying to work out how to get the value from one setting in
I'm trying to work out how to get a custom route working with rails
I'm trying to work out if it is possible to use Google Apps Scripts
I'm trying to work out whether it is possible to change the icon based
I am trying to work out if it is possible to use scrollable, part
I'm trying to work out if this is possible, let me give an example.
Possible Spoiler Warning I've spent ages trying to improve this algorithm and work out
I am trying work out with MERGE statment to Insert / Update Dimension Table
I trying to work out what regex will match filenames in quotes. Eg. blah
i am trying to work out the following problem in Java (although it could

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.