Skip to main content

@PersistenceContexts error

5 replies [Last post]
killy9999
Offline
Joined: 2006-11-08

Hi guys, as you can guess I have a problem. I'm creating an project using J2EE technology with EJB3. I need to use four persistence units. I have facades generated by netbeans to operate on entity objects. My problem is that I need one facade to be accesible by two persistence contexts using the same entity manager. With only one persistence context the whole thing was simple:

@PersistenceContext
private EntityManager em;

and it worked fine. Now that I need more persistence units to perform exactly the same methods I tried this:

@PersistenceContexts({
@PersistenceContext(unitName="ssbd18admin"),
@PersistenceContext(unitName="ssbd18klient") })
private EntityManager em;

Which gives me compilation error:

'annotation type not applicable to this kind of declaration'

I've got no idea why this doesn't work. I looked through google and Oracle's JPA documentation and this should work. I also tried giving each persistence conetxt a name but it gives me the same error. What am I doing wrong?? Methods in the facade look like this:

public void create(DaneOsobowe daneOsobowe) {
em.persist(daneOsobowe);
}

that is they use automatically generated entity manager, without getting one from entity manager factory. I will greatly appreciate your help, as I am out of ideas :(

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ksak
Offline
Joined: 2005-05-20

You can't have one EntityManager that is associated with a persistence context from two different persistence units at the same time.

Regarding the specific error, the
@PersistenceContexts annotation can only be applied at the class-level.
It's used to define a PersistenceContext dependency that will be looked up
via java:comp/env (or EJBContext.lookup()). When using @PersistenceContexts, each embedded @PersistenceContext must declare
the name() attribute.

killy9999
Offline
Joined: 2006-11-08

> Regarding the specific error, the
> @PersistenceContexts annotation can only be applied
> at the class-level.

That was it! Big thanks. Now it seems to work. I mean that it compiles, can't check if it really works, since the role maping in my aplication isn't done yet :/

killy9999
Offline
Joined: 2006-11-08

Ok, seems that my enthusiasm whas a bit to fast :(

At first I did something like this:

@PersistenceContexts({
@PersistenceContext(name="klient",unitName="ssbd18klient"),
@PersistenceContext(name="admin",unitName="ssbd18admin"),
})

public class DaneOsoboweFacade implements DaneOsoboweFacadeLocal {

private EntityManager em;

public void create(DaneOsobowe daneOsobowe) {
em.persist(daneOsobowe);
}
}

Above code is only a part of the whole class. This doesn't work. I searched in tutorials and, just like you wrote ksak, I have to do something like this within a method:
em = (EntityManager)ctx.lookup("name-of-unit");

The problem is that i need to use different name-of-unit depending on the caller. Is there any way to this automatically, or am I supposed to do something like this in the class constructor:

if (ctx.isCallerInRole("KLIENT") {
em = (EntityManager)ctx.lookup("klient");
} else if (ctx.isCallerInRole("ADMIN") {
em = (EntityManager)ctx.lookup("admin");
}

I know this may be the basic question, but I'm just learning java persistence technology.

ksak
Offline
Joined: 2005-05-20

The name() attribute refers to the location within the component's private naming context (java:comp/env). If you're doing a lookup using the
InitialContext(), it would be

InitialContext ic = new InitialContext();
em = (EntityManager) ic.lookup("java:comp/env/klient") or
em = (EntityManager) ic.lookup("java:comp/env/admin")

If the code is running within an EJB component, you can also do a lookup
using the special EJBContext.lookup() API. Those lookups are already
scoped directly to java:comp/env, so in that case it would be

@Resource private SessionContext sCtx;

void foo() { ...
em = (EntityManager) sCtx.lookup("klient");

killy9999
Offline
Joined: 2006-11-08

Ok, everythng seems to be fine. Big thanks one more time.