Skip to main content

[glassfish embedded] Retrieving EntityManager in the Jersey XmlAdapter

2 replies [Last post]
jszczepankiewicz
Offline
Joined: 2008-01-17

I'm using glassfish 3.0.1 embedded (snapshot from 08-Jun-2010) for testing and standalone glassfish 3.0.1 stable for production. I have to access EntityManager for read operations in the XmlAdapter. I have tried two ways which both work on standalone 3.0.1 glassfish:

1. Using method:
EntityManager em = Persistence.createEntityManagerFactory("brexPU").createEntityManager();

2. Using injection of PU into JNDI and retrieving it in XmlAdapter according to information from:
http://download.oracle.com/docs/cd/B32110_01/web.1013/b28221/servjndi010...

Both above ways works in standalone glassfish. But unfortunatelly both fails in the glassfish embedded :(.

First method throws RE:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named brexPU

Second method can not found JNDI (the JDNI probably only contains the object:
java:comp/UserTransaction which is not enough for me):

EntityManager entityManager = (EntityManager) initCtx.lookup(
"java:comp/env/persistence/brexPU");

In order to properly test my application i have to have JAX-RS working properly. How can I access EntityManager in the XmlAdapter in the glassfish embedded.
Thanks in advance.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Marina Vatkina

Failure of the 1st option (and may be the 2nd) is most probably a classpath
issue. How do you start embedded server?

thanks,
-marina

glassfish@javadesktop.org wrote:
> I'm using glassfish 3.0.1 embedded (snapshot from 08-Jun-2010) for testing and standalone glassfish 3.0.1 stable for production. I have to access EntityManager for read operations in the XmlAdapter. I have tried two ways which both work on standalone 3.0.1 glassfish:
>
> 1. Using method:
> EntityManager em = Persistence.createEntityManagerFactory("brexPU").createEntityManager();
>
> 2. Using injection of PU into JNDI and retrieving it in XmlAdapter according to information from:
> http://download.oracle.com/docs/cd/B32110_01/web.1013/b28221/servjndi010...
>
> Both above ways works in standalone glassfish. But unfortunatelly both fails in the glassfish embedded :(.
>
> First method throws RE:
> javax.persistence.PersistenceException: No Persistence provider for EntityManager named brexPU
>
> Second method can not found JNDI (the JDNI probably only contains the object:
> java:comp/UserTransaction which is not enough for me):
>
> EntityManager entityManager = (EntityManager) initCtx.lookup(
> "java:comp/env/persistence/brexPU");
>
> In order to properly test my application i have to have JAX-RS working properly. How can I access EntityManager in the XmlAdapter in the glassfish embedded.
> Thanks in advance.
> [Message sent by forum member 'jszczepankiewicz']
>
> http://forums.java.net/jive/thread.jspa?messageID=475095
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

jszczepankiewicz
Offline
Joined: 2008-01-17

Hi, thanks for your reply

Regarding 1st method / problem it is 99,99 % not a classpath problem because all of the rest of my tests on glassfish are passing through, they are retrieving SessionBeans from JNDI and operate on Entity Beans or I invoke rest client which connects to Http on embedded and invokes SessionBeans with jax-rs annotations, so if there will be problems with classpath they will also raise issues but nothing like that happens.

Regarding 2nd method / problem if this will be the problem with the classpath I will not be able to deploy the war on embedded. I think this is not the direct classpath problem because I can invoke the beans by JNDI.

Method of invoking embedded server (imports are from org.glassfish.api.embedded.* or org.glassfish.api.*):

Server.Builder builder = new Server.Builder("embeddedTestServer");

EmbeddedFileSystem.Builder efsb = new EmbeddedFileSystem.Builder();
efsb.instanceRoot(new File(INSTANCE_ROOT));

EmbeddedFileSystem efs = efsb.build();
builder.embeddedFileSystem(efs);
Properties properties = new Properties();
//properties.put("java.util.logging.config.file", "conf/logging.properties");
server = builder.build(properties);

//ContainerBuilder containerBuilder = server.createConfig(CONTAINER_TYPE);
server.addContainer(ContainerBuilder.Type.all);
//containerBuilder.create(server);
try {
server.createPort(SERVER_PORT);
server.start();
} catch (Exception e) {
throw new RuntimeException(e);
}

//// and then deploying
deployer = server.getDeployer(); // type is EmbeddedDeployer
DeployCommandParameters deployParams = new DeployCommandParameters();
deployParams.name = applicationName;

File archive = new File(archiveName);
applicationName = deployer.deploy(archive, deployParams);

In my opinion there is something wrong with exporting some resources to the JNDI, or the deployer for glassfish embedded works differently from production version. Structure of my war:
WEB-INF/classes - containes Session Beans, Entity Beans, XmlAdapters etc.
WEB-INF/classes/META-INF - contains (persistence.xml + orm.* files, I am using mostly xml entities).

structure of my persistence.xml:
-----------------------------------------------------------------------------------

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"
version="1.0">
org.eclipse.persistence.jpa.PersistenceProvider

jdbc/brex

META-INF/orm.xml
META-INF/orm-platform.xml
META-INF/orm-filesystem.xml
META-INF/orm-emailqueue.xml
META-INF/orm-validation.xml
META-INF/orm-crm.xml
META-INF/orm-meta.xml

-----------------------------------------------------------------------------------

thanks in advance