Glassfish 3.0.1 + bundled EclipseLink (2.0.1) Lazy loading OneToOne [message #560235] |
Tue, 21 September 2010 15:58 |
Jack Mising name Messages: 11 Registered: September 2010 |
Junior Member |
|
|
Hi,
we currently develop a J2EE enterprise application and using Glassfish 3.0.1 with the bundled EclipseLink. I think its 2.0.1. In our application we have several OneToOne relationships between Entitys and we defined them all with @OneToOne(fetch = FetchType.LAZY). But when analyzing the logs we saw that these relations were fetched eagerly by EclipseLink.
So we created two test entitys as follows:
TestA.class
@Entity
@Table(name = "testa")
public class TestA implements Serializable {
@Id
@SequenceGenerator(name="TESTA_ID_GENERATOR", sequenceName="TESTA_ID_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TESTA_ID_GENERATOR")
private Long id;
public TestA() {
}
public Long getId() {
return this.id;
}
}
TestB.class
@Entity
@Table(name = "testb")
public class TestB implements Serializable {
@Id
@SequenceGenerator(name="TESTB_ID_GENERATOR", sequenceName="TESTB_ID_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TESTB_ID_GENERATOR")
private Long id;
//uni-directional one-to-one association to TestA
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "testa_id")
private TestA testa;
public TestB() {
}
public Long getId() {
return this.id;
}
public TestA getTesta() {
return this.testa;
}
public void setTesta(TestA testa) {
this.testa = testa;
}
}
Now when we call EntityManager.findById(TestB.class, id) the logs show that EclipseLink first fetches TestB from the database and then the associated TestA. When we then call TestB.getTesta() no database query will be performed because EclipseLink already knows the associated TestA.
The EntityManager we used in our application is not container managed. Our application needs to switch customer databases dynamically based on the web URL (www.example.com/customer1 results in an EntityManagerFactory with a datasource for the database of customer1). Here is the code:
public EntityManager aquireEntityManager(String uniqueCustomerName) {
EntityManagerFactory emf = this.emfMap.get(uniqueCustomerName);
if(emf == null) {
Map<String,String> emProperties = new HashMap<String, String>();
emProperties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, "jdbc/PostgreSQL_backend-" + uniqueCustomerName);
emf = Persistence.createEntityManagerFactory("backend-jpaPU", emProperties);
emf.getCache().evictAll();
this.emfMap.put(uniqueCustomerName, emf);
}
EntityManager em = emf.createEntityManager();
return em;
}
As we create our EntityManager "by hand" do we need to tell EclipseLink to use dynamic weaving on our entities so that OneToOne lazy loading will work? If so, how should we do it? We tried adding <property name="eclipselink.weaving" value="true"/> to our persistence.xml but this causes an exception: org.eclipse.persistence.exceptions.EntityManagerSetupExcepti on Exception Description: Value [true] for the property [eclipselink.weaving] is incorrect when global instrumentation is null, value should either be null or false.
So how can we activate lazy loading in our scenario? We thought that dynamic weaving is the default behavior when EclipseLink is used in an J2EE context like Glassfish.
|
|
|
|
|
|
|
Re: Glassfish 3.0.1 + bundled EclipseLink (2.0.1) Lazy loading OneToOne [message #594168 is a reply to message #588031] |
Tue, 21 September 2010 18:29 |
Fericit Bostan Messages: 68 Registered: June 2010 |
Member |
|
|
The default fetch type behavior in GlassFish 3.x, with EclipseLink, is to lazily load your associations. However, as you have stated, your not exactly using the EntityManager in the default fashion. For that reason, I would imagine that it is best to be explicit and define all of the behavior you expect in your persistence.xml file.
While dynamic weaving is absolutely wonderful (simply because it works without having to do anything) the recommended approach for weaving is statically. Usually, dynamic weaving is for development purposes only. Seeing as you can have ant or maven perform the static weaving for you every time the project is built, or Eclipse can perform it for you also if using ant, maven or even Dali, there is really no reason to not perform the static weave. You're going to need it once you go into production anyway so why not simply bring it into the build process from the beginning.
http:// wiki.eclipse.org/Introduction_to_EclipseLink_Application_Dev elopment_%28ELUG%29#Using_Weaving
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04011 seconds