I have a unit test set up with Arquillian and a remote Glassfish instance:
#RunWith(Arquillian.class)
public class ClientTest {
#EJB
private ClientService client;
#Deployment
public static Archive<?> createDeployment() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(Client.class.getPackage())
.addPackage(ClientService.class.getPackage())
.addPackage(Client_.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
#Test
public void testCreate() {
Assert.assertNotNull("Client not null", client);
Client c = client.getClientById(1L);
assertNotNull(c);
}
My persistence.xml looks as follows (works with development code)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="primary">
<jta-data-source>jdbc/ora</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.ddl-generation" value="none"/>
</properties>
</persistence-unit>
</persistence>
The data-source is configured on the remote glassfish-server. However I always get the following exception:
Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
you can directly point to your persistence.xml file from your source too.
.addAsResource("META-INF/persistence.xml")
that should alleviate your issues.
The line
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
tells Shrinkwrap to look for a test-persistence.xml file inside your test/resources folder and add it to your war file inside META-INF folder.
You must rename the persistence.xml file to test-persistence.xml and make sure that it is available in your test/resources folder
thanks for your questions. The persistence.xml was correctly deployed - I double checked it. The solution was to restart my glassfish server. Don't know why but after a restart everything worked fine.
Related
In an ear-file I have a persistence-unit for managing entities inside that ear which works fine. Additionally, there is a commons jar-file located in APP-INF/lib which is a dependency in other ears as well (other ear-modules on the same level). In this commons-jar I would like to have a single entity and create the possibility for CRUD on that entity. Now it is a jar only with an additional persistence unit (but same db as for the ear) and I can't manage to set it all up. Would that persistence unit be managed as well by the container? Could I use EJBs inside that jar file as well? Is it possible at all to do what I want?
I created the entity as follows
#Entity
public class ConfigEntity {
#Id
private Long id;
#Version
private Long version;
[further fields, getters and setters omitted]
}
With the following "Dao" I try to call isEntityManagerNull from a bean inside the ear.
public class ConfigBEDao {
#PersistenceContext(unitName = "configurationPU")
EntityManager entityManager;
public boolean isEntityManagerNull() {
return entityManager == null;
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="configurationPU" transaction-type="RESOURCE_LOCAL">
<description/>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/db</jta-data-source>
<properties>
<property name="eclipselink.target-database" value="${database.dialect}"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.cache.shared.default" value="false"/>
</properties>
</persistence-unit>
</persistence>
From a bean inside the ear file I call
ConfigBEDao dao = new ConfigBEDao();
dao.isEntityManagerNull();
which gives me the following exception already when trying to deploy on the server (payara):
java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [ConfigBEDao/entityManager] in the scope of the module called [main_ear-1.0-SNAPSHOT#main_ejb-1.0-SNAPSHOT.jar]. Please verify your application.
at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:733)
at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.findReferencedPUs(EjbBundleDescriptorImpl.java:889)
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:186)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
Then I changed the persistence.xml as follows but that didn't work either although the application can be deployed:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="configurationPU" transaction-type="RESOURCE_LOCAL">
<description/>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/db"/>
<property name="javax.persistence.jdbc.user" value="db"/>
<property name="javax.persistence.jdbc.password" value="secret"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
</properties>
</persistence-unit>
</persistence>
which gave me
javax.persistence.PersistenceException: No Persistence provider for EntityManager named configurationPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
I'm developing a first application on openshift. Its a jsf application with database connectitivity.
First i tryed onli some jsf xhtml page without jpa and all works.
When i insert a bean to acces to the database i have the message from the server "failed deployments ./ROOT.waer. Precisely when I insert this code something goes wrong :
A class for view intents
#ManagedBean(name="utnavctrl" ,eager=true)
#SessionScoped
public class Utnavctrl {
boolean newrecord=false;
#EJB
private Usersdao usersdao;
public Utnavctrl(){
A bean class for db connection
#Stateless
#LocalBean
public class Usersdao {
#PersistenceContext(unitName = "primary")
private EntityManager em;
public Usersdao() {
// TODO Auto-generated constructor stub
}
public List<User> getAllUsers() {
return em.createNamedQuery("User.findAll", User.class)
.getResultList();
}
I can't understand why after adding these two class (without modifing the view side xhtml ecc) the program doesn't work anymore.
The persince.xml is
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/kitchensink-quickstart-ds.xml, you
can find it in the source at src/main/webapp/WEB-INF/kitchensink-quickstart-ds.xml -->
<jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>
<class>com.antoiovi.gestcars.model.Automobili</class>
<class>com.antoiovi.gestcars.model.Group</class>
<class>com.antoiovi.gestcars.model.Prenotazioniauto</class>
<class>com.antoiovi.gestcars.model.Proglav</class>
<class>com.antoiovi.gestcars.model.Role</class>
<class>com.antoiovi.gestcars.model.User</class>
<class>com.antoiovi.gestcars.model.UserData</class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
Thhe bean.xml is
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file can be an empty text file (0 bytes) -->
<!-- We're declaring the schema to save you time if you do have to configure
this in the future -->
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</bean
Can anybody help me?
Remove eager=true because this creates problems with OpenShift...I do not know why but I have seen.
I'm nearly new to JavaEE (JRE7/JDK1.7.0) and startet my first (maven based) Project on Eclipse (Luna).
Applicationserver is JBoss EAP 6.2.0.GA (AS 7.3.0.Final-redhat-14) which I updated to JSF2.2 by adding jsf-api-2.2.7.jar and jsf-impl-2.2.7.jar and registering in module.xml - which works perfect, by the way.
The problem/question is, why can't I get the EntityManager from the container (AS) with Injection, with following code in my Project:
#Stateless
public class SCatRep {
#PersistenceContext
EntityManager em;
private ...
*In persistence.xml there is only one persistenceunit defined. So no further arguments are necessary ... in my opinion?!
On the other hand, when I try to get the EntityManager from container with a lookup, the EntityManager is present and everything works fine. I used this code (for testing):
#Stateless
#PersistenceContext(unitName="scha", name="persistence/em")
public class SCatRep {
EntityManager em;
public SCatRep() {
try {
Context ic = new InitialContext();
em = (EntityManager) ic.lookup("java:comp/env/persistence/em");
} catch (NamingException e) {
e.printStackTrace();
}
}
I read a lot forums and tutorials - because this behaviour is discussed a lot - but can't get a solution or answer. Hope somebody can help me and point me to the right direction.
Thanks in advance.
EDIT:
I'm using EJB 3.1. And here is the persistence.xml:
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="scha" transaction-type="JTA">
<jta-data-source>java:/scha_jndi</jta-data-source>
<properties>
<!-- SQL dialect -->
<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class" value="thread" />
<!-- Echo all executed SQL to stdout -->
<property name="show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Thanks for help.
I tried to access the EntityManager in constructor ... which is not possible, due to the initializationprocess.
Now I put the Initial loading (databaseaccess) in a #PostConstruct-annotated method and this works perfectly.
I have been trying to get a very basic standalone JPA example to work with Postgres using the Eclipse IDE.
I have a persistance.xml defined which looks like this.
<?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="sample" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>package.class</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.jdbc.driver" value="org.postgresql.Driver" />
<property name="eclipselink.jdbc.url"
value="jdbc:postgresql://localhost:5432/sample" />
<property name="eclipselink.jdbc.user" value="scott" />
<property name="eclipselink.jdbc.password" value="tiger" />
</properties>
</persistence-unit>
</persistence>
This file lives in the src/main/resources/META-INF folder. I have included the src/main/resources folder to my source directory in eclipse. I have one simple Entity defined named User. When I try and create that entity
EntityManagerFactory entityManagerFactory
= Persistence.createEntityManagerFactory("sample");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
User user = new User();
user.setEnabled(false);
user.setEmailId("test#test.com");
tx.begin();
em.persist(user);
tx.commit();
} catch (Exception e) {
em.getTransaction().rollback();
} finally {
em.close();
}
I get the following exception - Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named sample
It seems like my persistance.xml file is not being picked up. Where does the JPA framework look to load the persistance.xml file?
This was a dumb mistake. The file needs to called persistence.xml and not persistance.xml.
We've been working on this for days and we are stumped. This is supposed to be an easy tutortial using TopLink. We are trying to get this to work before we do our real web app. This is the following exception we get:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu1:
The following providers:
oracle.toplink.essentials.PersistenceProvider
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at client.Client.main(Client.java:45)
Java Result: 1
this happens after executing this line from our emf driver class:
emf = Persistence.createEntityManagerFactory("pu1");
I'm assuming the problem is in our persistence.xml file (which is in the correct folder (WEB-INF/classes/META-INF). Also netbeans generated the xml file for us which 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="JPAExamplePU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>SomeDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
We were also thinking it may be an adding a library issue or something along that line. Any help is much appreciated. Thanks
You have the wrong persistence unit name. Use the one from the xml (i.e. the one defined with <persistence-unit name="..."):
emf = Persistence.createEntityManagerFactory("JPAExamplePU");