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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:56:41+00:00 2026-06-03T02:56:41+00:00

To start I have two entities. User.java @Entity public class User { @Id private

  • 0

To start I have two entities.

User.java

@Entity
public class User { 
    @Id
    private String username;
    private String password;
    @OneToMany(mappedBy = "to", cascade=CascadeType.PERSIST, orphanRemoval=true)
    private ArrayList<Message> messages;
}

public void deleteMessage(Message message){
        System.out.println(messages);
        messages.remove(message);
        System.out.println(messages);
}

Message.java

@Entity
public class Message {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private String id;
    @ManyToOne
    @NoGson
    private User to;
}

The ArrayList messages contains messages for that user and every message in that list has as the “to” field that user. All this is persisted correctly. When I however run the deleteMessage method the first sysout gives the full list correctly and the seconds sysout just gives null instead of the list without the deleted message.

I don’t know if it has something to do with jpa or if I’m just making a stupid mistake but I can’t find the reason why the remove method just removes the entire ArrayList. When I call the deleteMessage function I also call the remove method of the entity manager to delete the message from the database.

Update

After more testing I noticed that the second sysout does give the correct output. So the problem isn’t the remove method from the ArrayList. But the arraylist becomes null after executing the following method.

public void deleteMessage(Message message) {
    em.remove(message);
} 

So now I know it is a JPA problem.

  • 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-03T02:56:42+00:00Added an answer on June 3, 2026 at 2:56 am

    I solved the problem by resetting the to field of the message that has to be deleted to null. This way the bidirectional connection is broken. If I run the deleteMessage method from above again the ArrayList doesn’t change to null and everything works fine.

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

Sidebar

Related Questions

I have the following two entities: public class Field { public int FieldID {
I have an activity with two buttons, start and stop. If the user press
I have two classes an Arc class and a Line class public class Arc
I have two entities event and time. The event entity has a 1 to
I have two date entities, start_date and end_date and now I want to find
I have two datepickers. When start date is selected, end date is automatically assigned
I have two schemas S1 and S2 with identical table structure. I`ll start with
I have two points in a cathesian 2D system, both give me the start
I'm using the jQuery datepicker where I have two textboxes (start date / end
I need change background of all text that have two spaces from the start

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.