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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:23:16+00:00 2026-06-14T04:23:16+00:00

I have two entities: @Entity public class Entity1{} @Entity public class Entity2{ @OneToOne protected

  • 0

I have two entities:

    @Entity
    public class Entity1{}

    @Entity
    public class Entity2{
        @OneToOne
        protected Entity1 e1;
    }

I have one method to search for Entity1

    Entity1 findEntity1(some args){
        EntityManager em = this.emfp.getEntityManager();
        //perform search
        return e1;
    }

I use this method to find Entity2

    Entity2 findEntity2(some args){
        EntityManager em = this.emfp.getEntityManager();
        e1 = findEntity1(args);
        //perform search using e1 :  Entity2.e1 = e1
        return e2;
    }

Each method has it’s own EntityManager and therefore it’s own Persistence context.
Could I use e1, returned from first method, in my second method?

  • 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-14T04:23:18+00:00Added an answer on June 14, 2026 at 4:23 am

    While you cannot do it the way you are describing it – having different persistence contexts, you could do some tweaks to the code, effectively putting the entities in the same context. It depends on your stack if you can use all of them:

    For an Java EE application, start the search from a session bean and use a container-managed EntityManager – this will wrap the persistence context in a transaction and both entities will be kept in the same context for the duration of the transaction.

    For a Java SE application, you could still use transactions – UserTransactions. You would need to control the span of the transaction manually though.

    Use an EXTENDED persistence context (since JPA 2.0). It only gets invalidated explicitly, so all the fetched entites will remain inside the context until you say so.

    For all these solutions, you would need to change the EntityManager retrieval. Use the @PersistenceContext annotation in Java EE context and
    @PersistenceUnit to retrieve the EntityManagerFactory in SE context

    EDIT: It does not matter in how many EJBs you divide the search as long as you are staying inside the same transaction. As a rule of thumb, a transaction starts when the first EJB method is called and ends when this method returns. All methods invoked by this method will be in the same transaction.
    This default behaviour may change though – the transactionality of EJB methods is defined by
    @TransactionAttribute
    annotations, so simply follow the path of the call along your beans.

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

Sidebar

Related Questions

A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
I have two entities, Entity1 and Entity2, on a one to many relationship. On
I have the following two entities: public class Entity1 { public IList e2 {
We have the following two entities with many-to-many association: @Entity public class Role {
I have two entities @Entity public class Tabulka{ @OneToMany(mappedBy = tabulka) private List<VysledkyHraca> vysledkyHraca;
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have two related entities, say @Entity public class Book { @ManyToOne Shelf shelf;
I have two entities: @Entity public class File ....... @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id;
I have two JPA entities: @Entity public class TaskSchedule { ... private String name;

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.