Basic Standalone JPA example with Postgres using Eclipse - eclipse

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.

Related

how to set UserTransaction Eclipselink/Glassfish in multitenancy web-app

i spend a lot of time arround this question my project deals with WEB-APP/multitenancy
Eclipselink
Glassfish v4.1
Persistence Usage for Multiple Tenants : Persistence Unit per Tenant (according to eclipselink docs : Persistence Usage for Multiple Tenants
the persistence.xml si set as follow :
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/mydata</jta-data-source>
<mapping-file>META-INF/eclipselink-orm.xml</mapping-file>
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.multitenant.tenants-share-cache" value="false" />
<property name="eclipselink.exclude-eclipselink-orm" value="false"/>
<property name="objectdb.query-language" value="JPA" />
<property name="javax.persistence.lock.timeout" value="5000" />
</properties>
</persistence-unit>
</persistence>
inside a listener i put every new created EntityManagerFactory per tenant in a MAP and i call a static method to obtain an entity manager when needed
public static EntityManager createEntityManager(String tenant){
if(mapEMF.containsKey(tenant)){
return mapEMF.get(tenant).createEntityManager();
}else{
HashMap<String, String> properties = new HashMap<>();
properties.put(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT, tenant);
properties.put(PersistenceUnitProperties.MULTITENANT_SHARED_EMF, "false");
properties.put(PersistenceUnitProperties.SESSION_NAME, tenant);
properties.put(PersistenceUnitProperties.CONNECTION_POOL_USER, "dbuser");
properties.put(PersistenceUnitProperties.CONNECTION_POOL_PASSWORD, "dbpass");
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, properties);
mapEMF.put(tenant, emf);
return emf.createEntityManager();
}
}
my big question is how to obtain a transaction !
i have used but with no success
#Resource UserTransaction trasaction;
have created UserTransaction
transaction = (UserTransaction)new
InitialContext().lookup("java:comp/UserTransaction");
with em.joinTransaction();
the only method that works is em.getTransaction.begin()/em.getTransaction.commit();
but according the API/docs getTransaction is used in RESOURCE_LOCAL witch is not recommended for java ee web-app where we are called to use JTA as transaction type
Please HELP.

database access from a jar inside ear

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)

Error No Script Specified when using JPA Persistence Schema Generation

Running on Wildly 8.2.0.Final. I get the following error when trying to generate a database schema from a script.
Caused by: javax.persistence.PersistenceException: Schema generation configuration indicated to include CREATE scripts, but no script was specified
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.mycompany_mavenFlowChartDB5_war_1.0-SNAPSHOTPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/postgresql</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name=""/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
<property name="javax.persistence.schema-generation.scripts.action" value="none"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="META-INF/create-script.sql"/>
</properties>
</persistence-unit>
</persistence>
Where the create script is located:
/src/main/resources/META-INF/create-script.sql
Thanks in advance!
The Wildfly persistence schema generator gui creates incorrect property names for the drop and create targets.
The correct property names are
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create-script.sql"/>
change create-script to create-script-source and drop-script to drop-script-source
source: https://github.com/hantsy/ee7-sandbox

How to use 2 PersistenceUnits in one Persistence.xml in a JavaEE application?

I am trying to access 2 different tables in my database.
Initially my persistence.xml was as follows:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="Persistence">
<jta-data-source>jdbc/classicmodels</jta-data-source>
<class>com.tugay.maythirty.model.Customers</class>
<class>com.tugay.maythirty.model.Products</class>
<class>com.tugay.maythirty.model.Employee</class>
<class>com.tugay.maythirty.model.Office</class>
</persistence-unit>
</persistence>
So I defined a DataSource in Glassfish following this great artical and my application was working fine. I was using #PersistenceContext annotation in my DAO classes with name="Persistence"
This basically is connected to a table called "classicmodels".
Then I needed to fetch some data from a table called "sakila". I added these lines to my persistence.xml:
<persistence-unit name="sakila">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tugay.maythirty.model.Actor</class>
<class>com.tugay.maythirty.model.Film</class>
<class>com.tugay.maythirty.model.FilmActor</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sakila"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
And I have used this code in my DAO:
public List<Actor> getAllActors(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila");
EntityManager em = emf.createEntityManager();
TypedQuery<Actor> actorTypedQuery = em.createQuery("Select a from Actor a",Actor.class);
return actorTypedQuery.getResultList();
}
When I deploy my application to GlassFish however I started getting this Exception:
java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [Persistence] in the scope of the module called [may-thrity]. Please verify your application.
So it seems that now my Persistence Unit with name "Persistence" is gone.
What is it that I am doing wrong?
Have you tried explicitly declaring your persistence unit with
#PersistenceContext(unitName="Persistence")
instead of
#PersistenceContext(name="Persistence")
PersistenceContext doc

JPA- No Persistence provider for EntityManager for MSAccess DB

Im trying to use JPA to connect to a MS access DB using a JDBC:ODBC connection. Here is my Persistance.xml code :
<?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="DSRJPA">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>com.entity.AccessEntity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:odbc:MSAccessDB"/>
<property name="toplink.jdbc.user" value="admin"/>
<property name="toplink.jdbc.driver" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<property name="toplink.jdbc.password" value="admin"/>
<property name="toplink.jdbc.read-connections.min" value="1"/>
<property name="toplink.jdbc.write-connections.min" value="1"/>
</properties>
</persistence-unit>
</persistence>
But when i run the code, i get this error :
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named DSRJPA: 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 com.test.Access.main(Access.java:19)
Please throw me some light on this error.
Thanks in Advance.
Have you referenced your persistence unit in the class?
i.e.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("DSRJPA");