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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:36:01+00:00 2026-05-16T21:36:01+00:00

I have got a onetomany relation in Hibernate: @Entity public class Project @OneToMany(cascade =

  • 0

I have got a onetomany relation in Hibernate:

@Entity 
public class Project
    @OneToMany(cascade = CascadeType.ALL)
    @OrderColumn(name = "project_index")
    List<Application> applications;


@Entity
public class Application

Now, I tried to update an application directly:

    session.merge(application);

The result is that a new Application is created, instead of updating the entity. How can I reatach my entity properly and update my entity?

Thank you


Solution:

@Override
public boolean equals(Object obj)  {
    if (!(obj instanceof Application)) {
        return false; 
    }
    Application app = (Application)obj;

    return id == app.getId();
}

@Override
    public int hashCode () {
        return status.hashCode();
}
  • 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-16T21:36:02+00:00Added an answer on May 16, 2026 at 9:36 pm

    When you call session.merge(), it returns an object which is the value of that entity in the context of the current session. You can’t simply reuse the existing object in a new session, because the object is outside of its session’s life (and a new object to represent the same entity may already have been created in the new session). You need to honor hibernate’s single-instance-per-entity-per-session semantics by using the new object in place of the old.

    If you find that calling merge() results in a new entity being created (an insert statement is generated on flush), this might mean that the identifier of the object you’re trying to merge doesn’t match, or there is an issue with your equals() or hashCode() implementation.

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

Sidebar

Related Questions

I have an entity Person: class Person { String name; String phone; @OneToMany(cascade =
I got a OneToMany relation between User and Group Group.java @Entity public class Group
I have got a class which is called BaseCore which just runs all the
I have got a StartCommand Class: public class StartCommand : ICommand { public void
I've got to Tables related in one-to-many association: Product*1 - n*Inventory @Entity public class
I have got entity: Entry { public virtual ICollection<Content> Contents { get; set; }
I have got some code to load an assembly and get all types, which
I am quite new to hibernate. I have got the following tables: Contract ContractID
I have got my HTML5 project a bit further along - I have one
I've got the following (simplified) problem: I have a entity in a Many-To-One relationship:

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.