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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:20:58+00:00 2026-05-30T01:20:58+00:00

I have this specific case that bring me some doubts: My class A have

  • 0

I have this specific case that bring me some doubts:
My class A have a one-to-many relationship with B:

A  1----->*  B

Which, as far as I know, makes their related instances belong to the same entity group.
I have to retrieve one particular A instance and one of its B, so I search for A and iterate through its B List to find the particular B (always searching by Id). After that, I change one attribute of A and B and commit my changes by merging A. So here are my questions:

  1. Knowing that I must retrieve A and B (because I have to modify both), should I make 2 searches instead of iterating the B List?
  2. Making 2 searches, modifications on B will be persisted if I only persist A?
  3. Will question 1 and 2 have the same answer in this case:
A  1----->*  C  1----->*  B

Complementing the question with some code:

@Entity
public class A
{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    private int aField;

    @OneToMany(cascade={CascadeType.ALL})
    private List<B> bList;

    //...
}

@Entity
public class B
{
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Key key;

     private int someBField;

     // no way to get A from here...

     //...
}

public void someService1(Key aKey, Key bKey)
{
     //...

     // Here, I'm sure that bKey is of an entity son of aKey entity (I trust the client)
     A a = entityManager.find(A.class, aKey);
     a.setAField(a.getAField()++);

     for(B b : a.getBList())
          if(b.getKey().equals(bKey))
                b.setSomeBField(b.getSomeBField()++);

     entityManager.merge(a);

     //...
}

public void someService2(Key aKey, Key bKey)
{
     //...

     // Here, I'm sure that bKey is of an entity son of aKey entity (I trust the client)
     A a = entityManager.find(A.class, aKey);
     a.setAField(a.getAField()++);

     B b = entityManager.find(B.class, bKey);
     b.setSomeBField(b.getSomeBField()++);

     entityManager.merge(a);

     //...
}

Well, both someService1 and someService2 do the same thing, if I did everything right. Which one is better?

  • 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-30T01:20:59+00:00Added an answer on May 30, 2026 at 1:20 am

    If you know the id (and you say you’re searching by id) then just do an em.find for the A, and em.find for the B. Two calls. Sadly the JPA API doesn’t have a em.findAll. JDO, on the other hand, does have pm.getObjectsById

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

Sidebar

Related Questions

I have seen a few posts regarding this issue but not one specific to
This is a bit of a specific request unfortunately. I have a CMS that
I have seen several similar questions to this, but none that addresses my specific
I have this function that loads an specific .log file: function loadTracks():void { tracksLoader.load(new
Ok, if I have this right, my code can get cookies at a specific
I have a specific DB2 query, and I would like to execute this query
This is quite a specific question, and I have had no luck on the
I have an array of ints like this: [32,128,1024,2048,4096] Given a specific value, I
This question is kind of related to another question but I have a specific
This question may seem a little bit stackoverflow-implementation specific, but I have seen a

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.