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 8182345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:50:10+00:00 2026-06-07T00:50:10+00:00

Final goal: Have a few java objects sharing the same base class persisted into

  • 0

Final goal:
Have a few java objects sharing the same base class persisted into a database while each one having its own self-contained table with all own/inherited objects, and a simple auto-generated by the database id.

Very simple requirement. Impossible (?) with Hibernate!

What I have so far (using MySQL, Hibernate XML mapping):

map.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field"
    default-lazy="false">

    <class name="xyz.url.hibernate.EntityBase" abstract="true">
        <id name="m_id" column="id">
            <generator class="identity" />
        </id>

        <version name="m_version" column="version" type="long" />

        <union-subclass name="xyz.url.hibernate.User" table="my_entity">
            <property name="name" column="name" type="string" />
        </union-subclass>
    </class>

</hibernate-mapping>

EntityBase.java

public abstract class EntityBase {
    private final long  m_id;
    private final long  m_version;

    public EntityBase() {
        this.m_id = 0;
        this.m_version = 0;
    }

    public long get_id() {
        return this.m_id;
    }

    public long get_version() {
        return this.m_version;
    }
}

User.java

public class User extends EntityBase {
    public String   name;
}

The above does not work unless you change the generator class to increment.
Currently this is the given error:

org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for: xyz.url.hibernate.User

Well, why does Hibernate ASSUMES I want a unique ID in a program-wide scope (I’ve read about some JPA requirement)… what a crap!

Anyway, I insist of having a simple table (per object) that aggregates all the object’s (User in this case) properties, and deny using discriminators (again what a crap..) which just complicate the final SQL queries and hit performance.

The only solutions I see here:

  1. Manually map all properties in a one block inside the XML.

  2. Map all properties while “importing” some <propert> items from an external file, thus achieve inheritance (reusage of properties). Possible? How to do?!?

  3. Explore annotations further which as far as I’ve seen they don’t support that simple inheritance requirement.

  4. Dump Hibernate and go with another ORM solution.

Please don’t link to the docs – I gave up on that one after reading them a few times!
An example of property import (from external file) would be great.

Thanks and god bless!

  • 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-07T00:50:11+00:00Added an answer on June 7, 2026 at 12:50 am

    First of all you need to decide whether your inheritance relationship should be mapped to the database (to allow polymorphic queries such as from EntityBase, polymorphic realtionships, etc) or not.

    As far as I understand in your case it shouldn’t be mapped, therefore it doesn’t make sense to use inheritance mapping options such as <union-subclass> at all. Now you have the following options:

    2. Hibernate doesn’t have special support for reuse of XML mappings, but its documentation suggests to use XML entities in this case, see, for example, 10.1.6. Table per concrete class using implicit polymorphism.

    3. Annotations certainly support this requirement in form of @MappedSuperclass annotation.
    This annotation can be used to mark a class that is not mapped to the database itself, but any mapping annotations defined on its properties take effect for its mapped subclasses, so that you don’t need to repeat them.

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

Sidebar

Related Questions

I am writing a translator, and have quite a few java String literals that
I have defined a Pipe class in Java, making use of PipedReader and PipedWriter
I have a maven multi-module project. The final goal wraps up all the libraries,
I have the following blockingQueue; final BlockingQueue<Message> blockingQueue = new LinkedBlockingQueue<Message>(); where public class
My final goal is to write the program which can run on the Unix
I want to extract kernel symbols from a u-boot image The final goal is
FINAL EDIT: After following the answer from Darin Dimitrov, I have found that the
I am trying to run the Tarjan java implementation from wikipedia . My final
I am working on saving twitter search results into a database (SQL Server) and
I have a C++ template class that contains a method pointer and a class

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.