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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:09:50+00:00 2026-06-10T23:09:50+00:00

I stumbled on a problem with merge method of EntityManager . I have this

  • 0

I stumbled on a problem with merge method of EntityManager.

I have this class

@Entity
public class QuestionList {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToOne
    private UserEntity user;

    @OneToMany(cascade = CascadeType.ALL)
    private List<QuestionUnit> units = new ArrayList<QuestionUnit>();

    public List<QuestionUnit> getUnits() {
        return units;
    }

    public void setUnits(List<QuestionUnit> units) {
        this.units = units;
    }

    public Long getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void addQuestionUnit(QuestionUnit QuestionUnit) {
        this.units.add(QuestionUnit);
    }

    public void setUser(UserEntity user) {
        this.user = user;
    }

    public UserEntity getUser() {
        return this.user;
    }

}

now, if I just modify one of the objects in units the merge is fine, but if I delete or add new object to the collection, the way that merge happens is that I am getting delete
from
QuestionList_QuestionUnit
where
QuestionList_id=?
and then every row is added one by one insert
into
QuestionList_QuestionUnit
(QuestionList_id, units_id)
values
(?, ?)

I am wondering, is it possible to tell JPA/Hibernate to just add/delete the row associated with the object added/deleted? Or is it the way that merge just works with collections and in my case I need to write my own query for updating the database.

I am asking because this collection can be potentially pretty huge, and just deleting all rows and inserting almost all of them again doesn’t seems like a good way to update the table.

EDIT: as requested:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class QuestionUnit {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private long id;

    @OneToOne(cascade = CascadeType.ALL)
    private AnswerUnit correctAnswer;

    public QuestionUnit(AnswerUnit correctAnswer) {
        this.correctAnswer = correctAnswer;
    }

    public void setCorrectAnswer(AnswerUnit correctAnswer) {
        this.correctAnswer = correctAnswer;
    }

    public AnswerUnit getCorrectAnswer() {
        return this.correctAnswer;
    }

    public long getId() {
        return id;
    }

    public void setId(long id){
        this.id=id;
    }

    public abstract Object getQuestionContent();

    public abstract String getTitle();

}

there are 3 concrete classes in this hierarchy, for example

@Question("openQuestion")
@Entity
public class OpenQuestion extends QuestionUnit {

    public void setQuestionContent(String questionContent) {
        this.questionContent = questionContent;
    }

    private String questionContent;

    public OpenQuestion() {
        this(null, new OpenQuestionAnswer());
    }

    public OpenQuestion(String questionContent, AnswerUnit correctAnswer) {
        super(correctAnswer);
        this.questionContent = questionContent;
    }

    public String getQuestionContent() {
        return this.questionContent;
    }

    public String getTitle() {
        return questionContent;
    }
}

@Transactional(propagation = Propagation.REQUIRED)
public void update(QuestionList questionList) {
    entityManager.merge(questionList);
}

this method is used for all updates, because the list is in the detached state.

  • 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-10T23:09:52+00:00Added an answer on June 10, 2026 at 11:09 pm

    It think the problem is caused because of using List instead of Set.

    @OneToMany(cascade = CascadeType.ALL)
    private List<QuestionUnit> units = new ArrayList<QuestionUnit>();
    

    change to

    @OneToMany(cascade = CascadeType.ALL)
    private Set<QuestionUnit> units = new HashSet<QuestionUnit>();
    

    Hibernate Many to Many Relations Set Or List?

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

Sidebar

Related Questions

I'm quite new to hibernate and have stumbled on this problem, which I can't
I am pretty new to Rails and I have stumbled upoun this problem. I'm
I stumbled onto this problem today, let me explain the issue: I have this
I stumbled upon this problem and it took my a while to realise what
I stumbled across this problem in F#. Suppose, I want to declare two types
I have recently stumbled upon a problem with selecting relationship details from a 1
I have been playing about with canvas, but have stumbled across a problem. When
I have stumbled upon a problem when calling a nested Async which happens to
With the new iPhone 2x files i've stumbled uppon this problem...
I'm doing some Delphi (2010) work this summer, and I've stumbled upon this problem:

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.