cancel
Showing results for 
Search instead for 
Did you mean: 

EntityManager injection fails when persistence unit defined in other DC

Former Member
0 Kudos

Hi experts,

I'm writing application that consists of two separate DCs. In the first DC I specify the JPA entities and in the second one I specify the session beans that use these entities. In the persistence.xml file in the second DC I have tag <jar-file>xxx.jar</jar-file> specifying the jar file of the first DC (the one with the entities). Both DCs are packed in one EAR. I use this app as back-end to one WebDynpro app. But when I start it I get an error that says that it can't inject value for my EntityManager cause can't take it from the factory. Is there something SAP specific that I need to add so the container could inject the EntityManager.

JPA DC                                           Session Bean DC
-------------------------------------------------------------------------------------------------------------------------------------------------------
class Entity{                                    class EntityBean implements EntityBeanLocal{
public String value;                          @PersistenceContext(unitName="myPersistenceUnitAsSpecifiedInTheXMLInTheJPADC")
public String getValue(){                 public EntityManager em;
    return this.value;                         public String getValue(long id){
}                                                            return em.find(Entity.class,id);
                                                        }

The annotations in the Entity class are intentionally skipped. The problem is that

em

field isn't injected.

Please help. Any ideas are appreciated. The good ones will be rewarded with points

Regards,

Martin

Accepted Solutions (0)

Answers (2)

Answers (2)

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi Martin

Where exactly the persistence.xml located in the second DC? Could you provide its source code?

BR, Siarhei

Former Member
0 Kudos

Hi Martin,

I have two questions:

1)

>

> In the persistence.xml file in the second DC I have tag

Does this mean you have two persistence.xml files - one per each DC?

2) Is there any particular reason that you are using a separate DC for your entities? You can use the EJB DC to store them, and this would really simplify the case.

Best,

Yordan

Former Member
0 Kudos

Hi Yordan,

Yes, indeed I have two persistence.xml files (one per DC) but I'm not sure whether it is necessary.

So both DCs are packed in one common EAR and they are at the same level (in one folder so to say). The persistence.xml in the DC with the session beans is located in ejbModule/META-INF folder and the beans are in package com.xxx.yyy located in ejbModule folder.

Actually I need to place the DC with the JPA entities in a different EAR (to allow for separate deployment) but according to the JPA spec this way the PU will not be visible for the session beans DC. Is that right?

Regards

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi

You definitely do not have to have two persistence.xml files. I cannot answer exactly why do you have such error, but try to join your two modules into single one. So you'll have one EJB module with PU and EJB; and one EAR.

Also check carefully all errors or warnings that occur during DC build (see build log). Sometimes important thing written as warnings that could lead you to the reason.

BR, Siarhei

Former Member
0 Kudos

So let's say I have a DC where I just specified the entities (the POJOs with @Entity annotation). In other DC I have the session beans and the persistence.xml file. Both DCs are packed in one EAR. What should be the content of persistence.xml file in order to inject EntityManager in the session beans? My current content is :

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
	xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="myPU" transaction-type="JTA">
		<jta-data-source>JPA_DS_ALIAS</jta-data-source>
		<jar-file>dcWithEntities.jar</jar-file>
		<properties>
			<property name="x"
				value="y" />
			<property name="z"
				value="15" />
		</properties>
	</persistence-unit>
</persistence>

The content of the DC with the session beans is:

DC.jar

|------package with beans

|------META-INF

........ |-------persistence.xml

........ |-------data-source-aliases.xml

What do I do wrong?

Former Member
0 Kudos

Hi Martin,

I made a couple of attempts to reproduce your case locally, but I have some problems with my application server and could not deploy successfully...

Presuming that I am not an expert in NWDI, I would suggest the following:

(below, I'm calling your two DCs "EJB DC" and "JPA DC")

1, Use a single persistence.xml file - put it in the JPA DC. Use the same persistence.xml configuration, but remove the

<jar-file>dcWithEntities.jar</jar-file>

line. Remove the persistence.xml of the EJB DC.

  • Another option would be to use the persistence.xml in the EJB DC without removing the jar-file line. Then, you'll probably need to remove the persistence.xml from the JPA DC or simply do not expose it as a public part (see 2. below)

2, Set dependencies between the two DCs. For more information, check this document: http://help.sap.com/saphelp_nwce711/helpdata/en/47/c53cf0243c5802e10000000a42189d/content.htm I think you have to expose the entity classes (and, perhaps, the persistence.xml file?) as public parts in the JPA DC, and then set a dependency from the EJB DC to the JPA DC.

Share the result here.

I hope that helps!

Best,

Yordan

Former Member
0 Kudos

Hi Yordan,

What you say is right but actually my major problem turn out to be missing db table. But 10x anyway. Fighting with this issue helped me better understand JPA