[jboss-user] [EJB 3.0] - Re: Setting persistence JNDI name for PersistenceContext

jaikiran do-not-reply at jboss.com
Tue Apr 29 04:30:29 EDT 2008

The @PersistenceContext does not accept an attribute like jndiName, where you could have specified the jndi-name of the entitymanager. 

However, i do see a way out. There's a @Resource annotation which accepts a mappedName attribute (which is nothing but the jndi-name of a resource) and also the "type" of the resource. So here's what you can do:

1) Bind the entitymanager to the JNDI. To do this, you have to add the following 2 properties to your persistence.xml:

  | <?xml version="1.0" encoding="UTF-8"?>
  | <persistence xmlns="http://java.sun.com/xml/ns/persistence">
  | 	<persistence-unit name="msp-par">
  | 		<jta-data-source>blahblahblah</jta-data-source> 
  | 		.....
  | 		.....
  | 		<properties>
  | 	        	 <property name="jboss.entity.manager.jndi.name" value="java:/MyEntityManager"/>
  | 	        	 <property name="jboss.entity.manager.factory.jndi.name" value="java:/MyEntityManagerFactory"/>
  |       		</properties>
  | 	</persistence-unit> 
  | </persistence>

2) In your bean, inject the persistence context as follows:

  | @Resource (type = EntityManager.class, mappedName = "java:/MyEntityManager")
  | private EntityManager entityManager;

Does this solve your problem?

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4147447#4147447

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4147447

More information about the jboss-user mailing list