Miller
Miller

Reputation: 491

Which Spring Entity Manager Factory should I use?

There are two entity manager factory beans in Spring that would work for my application. The org.springframework.orm.jpa.LocalEntityManagerFactoryBean and org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean. I am using Spring 3.0 with EclipseLink JPA 2.2.

What I've read about these two are that they are the same. Except that LocalContainerEntityManagerFactoryBean uses weaving. What is it? And, why would I want to use it?

Upvotes: 1

Views: 3533

Answers (2)

stratwine
stratwine

Reputation: 3701

"Weaving" is a term for program transformation, usually heard around "aspect oriented programming" area. The transformation is usually not done to the source but the .class (the bytecode) and a techy term for changing bytecode is "bytecode instrumentation".

why would I want to use it?

The JPA implementation you use may rely on such bytecode instrumentation for some features it provides and hence you might be forced into using it.

And for weaving to work correctly, you might need to specify a -javaagent: For eg,see section 'Eclipse Junit' here.

It looks like LocalContainerEntityManagerFactoryBean allows you to configure a weaver implementation ( one of

 DefaultContextLoadTimeWeaver, GlassFishLoadTimeWeaver, InstrumentationLoadTimeWeaver, OC4JLoadTimeWeaver, ReflectiveLoadTimeWeaver, SimpleLoadTimeWeaver, WebLogicLoadTimeWeaver

at an XML file, instead of relying on a -javaagent runtime argument.

This configuration isn't such a big factor, I'd guess.

Other features which the docs explain, sound like deciding factors.

LocalEntityManagerFactoryBean bootstrap is appropriate for standalone applications which solely use JPA for data access. If you want to set up your persistence provider for an external DataSource and/or for global transactions which span multiple resources, you will need to either deploy it into a full Java EE 5 application server and access the deployed EntityManagerFactory via JNDI, or use Spring's LocalContainerEntityManagerFactoryBean with appropriate configuration for local setup according to JPA's container contract.

Upvotes: 2

Travis
Travis

Reputation: 336

If you plan on deploying your application to an Application Server and letting the Application Server manage the Entity Manager Factory and Transactions than the LocalContainerEntityManagerFactoryBean might be a better option. If you rather have the Application be more isolated, than the LocalEntityManagerFactoryBean would be more appropriate.

This blog can help provide more insight: http://second-kind-demon.blogspot.com/2011/06/spring-jpa-java-ee-jboss-deployment.html

Upvotes: 1

Related Questions