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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:28:40+00:00 2026-05-24T12:28:40+00:00

I have a specific problem with an unit test using embedded OpenEJB container. I

  • 0

I have a specific problem with an unit test using embedded OpenEJB container. I have a bi-directional relation between two classes. In one direction the relation works properly, but in the opposite direction the relation works only in EAGER-mode. In LAZY-mode the field section stays null. The code snipped follows:

@Entity
@Table(name="tracks")
class TrackEntity implements Track {
    @Id
    private int trackNumber;
    @OneToMany(mappedBy = "track")
    private HashSet<SectionEntity> sections;

    public TrackEntity() {
        sections = new HashSet<SectionEntity>();
    }

    @Override
    public Collection<HistoricalEvent> getEvents() {
        if (sections == null)
            throw new CommonError("number=" + trackNumber, AppErrors.TRACK_EMPTY);

        TreeSet<HistoricalEvent> set = new TreeSet<HistoricalEvent>();
        for (SectionEntity se : sections)
            set.addAll(se.getEvents());

        return set;
    }
 }

My code is little bit specific. The class uses the field sections just internally to merge all sub-collections. I’m unable to fill sections lazily. I thing, the container expects client to access the field externally via a getter.

  • 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-24T12:28:41+00:00Added an answer on May 24, 2026 at 12:28 pm

    Its the problem with life cycle of enties. All enties (track and its sections) must be re-attached to the persistence context. The method collecting events must be in the class using EntityManager. (The entity cannot use the manager to re-attach itself.) Example of updated entity managing class follows:

    public class EntityDataAccessor {
        @PersistenceUnit(unitName = "someUnit")
        private EntityManagerFactory emFactory;
    
        //gets one track
        public Track getTrack(int number) {
            EntityManager em = emFactory.createEntityManager();
            try {
                return (Track)em.find(TrackEntity.class, number);
            }
            finally {
                em.close();
            }
        }
    
        //the new method collecting events
        public Collection<HistoricalEvent> getEventsForTrack(TrackEntity te) {
            EntityManager em = emFactory.createEntityManager();
            te = em.merge(te); //re-attach to the context
    
            Set<SectionEntity> sections = te.getSections();
            TreeSet<HistoricalEvent> set = new TreeSet<HistoricalEvent>();
            for (SectionEntity se : sections) {
                se = em.merge(se); //re-attach to the context
                set.addAll(se.getEvents());
            }
            em.close();
            return set;
        }
    }
    

    See question What’s the lazy strategy and how does it work? for more detail.

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

Sidebar

Related Questions

I have the problem that an specific step in Ant can only be executed
I have a specific changeset that I want to rollback my Development branch to,
The only SET parameter that I have found specific guidance for is SET TRANSACTION
I'd like to copy the files that have a specific file extension to a
I have a very specific html table construct that seems to reveal a Gecko
There have been several questions already posted with specific questions about dependency injection ,
I have some code which ignores a specific exception. try { foreach (FileInfo fi
I have an external stylesheet that has specific IE-hacks. Every so often my site
I have a certain service where specific functions will take longer to call than
Basically I have some code to check a specific directory to see if an

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.