JAXB eagerly receives fields marked by FetchType.LAZY even when the EntityManager has been cleared (), closed () and set to null

I bow to the apover stackoverflow community and humbly seek directions (I bow my head in submission when I write this)

I have the following Entity / bean class that has a mix of JPA / EclipseLink / JAXB / Moxy annotations: (btw EventBase is just @MappedSuperclass that contains extra fields)

@Entity @Table(name = "EVENTS") @XmlRootElement public class Event extends EventBase { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @XmlAttribute(name = "id") private long eventCID; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "APPLICATIONCID") private CustomerApplication customerApplication; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "CUSTOMERCID") private Customer customer; .... } 

Here is my code for marshaling this object (an external class is excluded for brevity)

 public static void main(String args[]) { Event event = myInstance.populateEvent(); myInstance.buildXMLFromEvent(event); } public Event populateEvent() { EntityManagerFactory emf = Persistence.createEntityManagerFactory(this.persistenceUnit); EntityManager em = null; Event event = null; try { em = emf.createEntityManager(); event = (Event) em.createQuery("Select object(e) from Event e where e.eventCID = 55000").getSingleResult(); em.clear(); em.detach(event); em.close(); em = null; emf.close(); emf = null; } catch (Exception e) { // just test code so catching general exception log.error("Unexpected error: " + e); } finally { if (em != null) { em.clear(); em.close(); } } return event; } private void buildXMLFromEvent(Event event) { System.out.println("Marshalling now:"); JAXBContext jc; try { jc = JAXBContext.newInstance(Event.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.valueOf(true)); JAXBElement<Event> jaxbElement = new JAXBElement<Event>(new QName("event"), Event.class, event); marshaller.marshal(jaxbElement, System.out); } catch (JAXBException e) { } } 

The generated xml really sends and readily retrieves all the member objects of the Event bean! ie) Customer, CustomerApplication, and any other mappings that I excluded for brevity. I use EclipseLink as my provider of JPA and Moxy for JAXB. What am I doing wrong here? You can see that not only the entityManager AND entityManagerFactory instances are cleaned, closed, and set to zero, but I also went ahead and detached the root Event entity. In addition, the fetchtype property is explicitly set to LAZY!

How is it that JAXB can look forward to when an Event object has been detached? I thought closing an entityManager by itself separates all managed objects? Is there any cached session context that JAXB hangs on? If so, why doesn't he even follow a clearly defined sampling strategy? Thank you very much in advance!

Ustad

+4
source share
1 answer

If eclipselink is offline, then ManyToMany and OneToMany relationships actually use lazy-load, for others, the fetch attribute is ignored and equal to EAGER.

Here is the documentation for this http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Lazy_Basics

Individually: By default, EclipseLink JPA ignores the selection attribute and applies javax.persistence.FetchType.EAGER by default.

Many-to-one: EclipseLink JPA performs lazy loading when the select attribute is set to javax.persistence.FetchType.LAZY.

Basic: By default, EclipseLink JPA ignores the selection attribute and applies javax.persistence.FetchType.EAGER by default.

This is why your entities are loaded with relationships.

Hope this helps.

+2
source

All Articles