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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:10:59+00:00 2026-06-17T05:10:59+00:00

I have used a class per hierarchy strategy for mapping the following class hierarchy

  • 0

I have used a “class per hierarchy strategy” for mapping the following class hierarchy in Hibernate.

I have Item as an abstract class.
The followings as concrete ones:
Book
Journal
DVD
CD

My mapping works perfect for saving and loading but it doesn’t work correctly on updating an item with a different class.
I’m using discriminators for each of the above classes.

my item.hbm.xml is like:

<!-- language: lang-xml -->
<hibernate-mapping>
    <class name="my.hibernate.items.Item" discriminator-value="item"
        table="testitem" catalog="efeu">
        <id name="id" type="java.lang.Long">
            <column name="id" />
            <generator class="native" />
        </id>
        <discriminator column="item_type" type="string" length="8" />
        <subclass name="my.hibernate.items.DVD" discriminator-value="DVD">
            <property name="duration" type="java.lang.Integer" column="duration" />
            <property name="trackNumber" type="java.lang.Integer"
                column="trackNumber" />
        </subclass>
        <subclass name="my.hibernate.items.Journal"
            discriminator-value="Journal">
            <property name="serialNumber" type="java.lang.Integer"
                column="serial_number" />
        </subclass>
    </class>
</hibernate-mapping>

and for updating I use this method:

<!-- language: lang-java -->
public static void updateItem(Item newItem){
    Session session = HibernateSessionFactory.getSession();
    Transaction tx = session.beginTransaction();

    session.update(newItem);// I tried merge too. it created a new row

    tx.commit();
    session.close();
}

There is no difference which class instance I send to updateItem method. All the fields are updated except the discriminator so if it is a book remains a book and I can’t convert it to CD, DVD or journal invoking the above method with an object of that kind as parameter.

I think it is clear what I am doing. I assum that I know that the row with id=111 in the database table is a Book object. So I do the following:

<!-- language: lang-java -->
Item cd = new CD();
cd.setId(111);
cd.setXXXProperties()
Item.updateItem(cd);

it is clear that cd is transient before invoking the method and I haven’t loaded the row with id=111 before that.

What should I do to make Hibernate change the discriminator column on invoking the update method?

  • 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-17T05:11:00+00:00Added an answer on June 17, 2026 at 5:11 am

    There is no way how to update a discriminator value. What you are experiencing is correct. Once object is created (as a CD or as a Book) it won’t change its type.

    Read more here: 5.1.6.1.1. Discriminator

    <discriminator
            column="discriminator_column"                      (1)
            type="discriminator_type"                          (2)
            force="true|false"                                 (3)
            insert="true|false"                                (4)
            formula="arbitrary sql expression"                 (5)
    />
    

    There is no update operation for discriminator.

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

Sidebar

Related Questions

I have the following problem: I have a base class (used as superclass) with
I'm used to the Java model where you can have one public class per
I have never used DBIx::Class until today, so I'm completely new at it. I'm
I have two Java interfaces and one implementing class. (I have used Eclipse to
I have created my custom MembershipProvider. I have used an instance of the class
In the pages so far, I have had used one connection class and one
I have a jar library. I used Java Class File Editor (CE) to modify
I have a class being used in node.js, and I want to pass various
I have a class with a std::map used as a container. I want to
I have a class named CommonCode that is used for stocking all methods I

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.