blob: 4011699cac9d72437dd22df609f05c59f4197404 [file] [log] [blame]
Title: persistence-unit-ref
Both lookup and injection of an EntityManagerFactory can be configured via
the @PersistenceUnit annotation or <persistence-unit-ref> in xml.
Annotations and xml have equal function in both lookup and injection.
<a name="persistence-unit-ref-InjectionandLookup"></a>
# Injection and Lookup
<a name="persistence-unit-ref-Viaannotation"></a>
## Via annotation
package org.superbiz;
import javax.persistence.PersistenceUnit;
import javax.persistence.EntityManagerFactory;
import javax.ejb.Stateless;
import javax.naming.InitialUnit;
@Stateless
public class MyBean implements MyInterface {
@PersistenceUnit(unitName = "bar-unit")
private EntityManagerFactory myBarEntityManagerFactory;
public void someBusinessMethod() throws Exception {
if (myBarEntityManagerFactory == null) throw new NullPointerException("myBarEntityManagerFactory not injected");
// Both can be looked up from JNDI as well
InitialContext unit = new InitialContext();
EntityManagerFactory barEntityManagerFactory = (EntityManagerFactory) context.lookup("java:comp/env/org.superbiz.MyBean/myBarEntityManagerFactory");
}
}
<a name="persistence-unit-ref-Viaxml"></a>
## Via xml
The above @PersistenceUnit annotation usage is 100% equivalent to the
following xml.
<persistence-unit-ref>
<persistence-unit-ref-name>org.superbiz.calculator.MyBean/myBarEntityManagerFactory</persistence-unit-ref-name>
<persistence-unit-name>bar-unit</persistence-unit-name>
<persistence-unit-type>Transaction</persistence-unit-type>
<injection-target>
<injection-target-class>org.superbiz.calculator.MyBean</injection-target-class>
<injection-target-name>myBarEntityManagerFactory</injection-target-name>
</injection-target>
</persistence-unit-ref>
# Lookup only
## Via annotation
package org.superbiz;
import javax.persistence.PersistenceUnit;
import javax.persistence.EntityManagerFactory;
import javax.ejb.Stateless;
import javax.naming.InitialUnit;
@Stateless
@PersistenceUnit(name = "myFooEntityManagerFactory", unitName = "foo-unit")
public class MyBean implements MyInterface {
public void someBusinessMethod() throws Exception {
InitialContext context = new InitialContext();
EntityManagerFactory fooEntityManagerFactory = (EntityManagerFactory) context.lookup("java:comp/env/myFooEntityManagerFactory");
}
}
<a name="persistence-unit-ref-Viaxml"></a>
# Via xml
The above @PersistenceUnit annotation usage is 100% equivalent to the
following xml.
<persistence-unit-ref>
<persistence-unit-ref-name>myFooEntityManagerFactory</persistence-unit-ref-name>
<persistence-unit-name>foo-unit</persistence-unit-name>
<persistence-unit-type>Transaction</persistence-unit-type>
</persistence-unit-ref>