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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:12:41+00:00 2026-05-25T00:12:41+00:00

I have 2 class Item & Type. An Item belongs to one type. It

  • 0

I have 2 class Item & Type. An Item belongs to one type.

It works ok. But when I try to change TYPE of an already saved item, it brings out the error:

identifier of an instance of com.myapp.model.Type was altered from 1
to 2.

It seems that Hibernate is thinking that I try to edit the id of Type. Actually, I want to change the Type of an item, not edit the type it is in. For a quick example, I have an Item name “Ball” belongs to “sport” type, but now I want to change it to “Tool” type. And Hibernate think that I want to change the name (and Id) of “Sport” type into “Tool”!

Pseudo code:

Item item = getItemFromDatabase(itemId);
item.setType(newType);
saveItem(item);

My mapping files:

<hibernate-mapping>
    <class name="com.myapp.model.Item" table="ITEM">
        <cache usage="read-write"/>
        <id name="id" column="ID">
            <generator class="sequence">
                <param name="sequence">item_seq</param>
            </generator>
        </id>

        <property name="name" column="NAME"/>
        <many-to-one name="type" 
             class="com.myapp.model.Type"
             column="type_id" 
             foreign-key="ITEM_TYPE_FK"
              />

        <property name="description" column="DESCRIPTION"/>
    </class>
</hibernate-mapping>

And

<hibernate-mapping>
    <class name="com.myapp.model.Type" table="TYPE">
        <cache usage="read-write"/>
        <id name="id" column="ID">
            <generator class="sequence">
                <param name="sequence">type_seq</param>
            </generator>
        </id>

        <property name="name" column="NAME"/>
    </class>
</hibernate-mapping>

Does I misunderstand anything?

UPDATE:

I have an Hibernate utility function like this:

    @Override
    @SuppressWarnings("unchecked")
    public <T> T getById(Class<T> entityClass, Serializable id) {
        try {
            Object result = this.sessionFactory.getCurrentSession().get(entityClass, id);
            if (result != null) {
                return (T)result;
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new DataAccessException(e);
        }
    }

Then in my code, I just call:

Type updatedType = repository.getById(Type.class, item.getType().getId());
  • 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-25T00:12:41+00:00Added an answer on May 25, 2026 at 12:12 am

    The way that I think is correct is

    Item item = getItemFromDatabase(itemId);
    item.setType(getItemTypeFromDatabase(newTypeId));
    saveItem(item);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a curious one. I have a class A. It has an item of
I have come to see an Installer class item in Visual studio. Why they
I have created a class for a dashboard item which will hold information such
I have a struct like this: class Items { private: struct item { unsigned
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class with a member function that takes a default argument. struct Class
I have been trying to write a recursive function that searches a stack, but

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.