I have a config file persistence.xml as mentioned below:
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="entity"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<validation-mode>NONE</validation-mode>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:src/test/resources/sql/entityDB;create=true" >
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="ALL" />
</properties>
</persistence-unit>
and load persistence code as:
public static void main(String[] args) {
String persistenceUnit = "entity";
Properties pros = new Properties();
pros.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
"src/test/resources/META-INF/persistence.xml");
// Get the entity manager for the tests.
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit,pros);
entityManager = entityManagerFactory.createEntityManager();
Query q = entityManager.createQuery("select * from entityTables");
List<Entity> todoList = q.getResultList();
entityManager.close();
}
But when I am calling createEntityManagerFactory I am getting bellow exception:
Exception in thread "main" javax.persistence.PersistenceException:No Persistence provider for EntityManager named entity
How can I fix it?
After <persistence-unit name="entity" transaction-type="RESOURCE_LOCAL">, use the persistence provider name:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
Edit : or put in the classpath of the manifest this packages : - persistence.jar - eclipselink.jar
Related
I want to set the values of my persistence.xml file with the values from my properties.config.
Is there any way to do this? Like any buildin function?
I think to a function like
factory.setvaluesfrompersistence(config.getpropertie("name"));
I want to do this bc I don't want to set my personal values in the persistence.xml so if I deploy this version there is no sesible data in there.
I use EclipseLink as JPA Propvider
my properties.config:
db.url=
db.user=
db.psw=
and my persistens.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="test"
transaction-type="RESOURCE_LOCAL">
<class>my.test.test</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="" />
<property name="javax.persistence.jdbc.url"
value="" />
<property name="javax.persistence.jdbc.user" value="" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
How I call it:
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
final EntityManager em = factory.createEntityManager();
final Query q = em.createQuery("select b from Beruf b");
final List<Beruf> BerufeList = q.getResultList();
for (final Beruf beruf : BerufeList) {
System.out.println(beruf);
}
em.close();
Solution
Is to create a Map where you set the key from persistence.xml file and give it a new value.
I'm trying to connect my mongoDb with JPA using Eclipselink, but got an error. I have tried a few things. But I got the same error every time. All imports are with Maven.
This is my Persistence.xml :
<?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="mongodb" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>eclipselink.Order</class>
<class>eclipselink.Item</class>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
<property name="eclipselink.nosql.property.mongo.port" value="27017"/>
<property name="eclipselink.nosql.property.mongo.host" value="127.0.0.1"/>
<property name="eclipselink.nosql.property.mongo.db" value="odm_eclipselink"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
En this is how I create my EntityManager
EntityManager em = Persistence.createEntityManagerFactory("mongodb").createEntityManager();
My error :
No Persistence provider for EntityManager named mongodb
It's weird because JPA create the database in mongodb. I check that in my commandprompt.
> show dbs
admin 0.000GB
local 0.000GB
odm_eclipselink 0.000GB
This is the (bottom) error from the stack trace:
Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:171)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:477)
...
The code is as follows (inspired by a #BalusC's example I found on SO today):
#Stateless
public class GradService {
#PersistenceContext
private EntityManager em;
public List<Grad> listAll() {
return em.createQuery("SELECT g FROM Grad g", Grad.class).getResultList();
}
}
As reference, this is my persistence.xml (webmodule-root:Configuration files:WEB-INFMETA-INF:persistence.xml
<?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="MojaPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/ekstraResource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
It seems, that you placed your persistence.xml file in wrong location. It should by located in /META-INF/ folder and not in WEB-INF.
Background
Plain old Java application, no web servers attached (not even JBoss), is using JPA to query a database.
Problem
The JDBC password is exposed in persistence.xml:
<?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="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql:DATABASE"/>
<property name="javax.persistence.jdbc.user" value="USERNAME"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.password" value="PASSWORD"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
</properties>
</persistence-unit>
</persistence>
Idea
It might be possible to instantiate a JNDI subcontext to set the password within the application's main method. This would possibly permit using a JTA data source:
<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="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
Question
How would you externalize the password such that it no longer exists inside persistence.xml?
Note: persistence.xml is stored in a public repository, so it would be nice if the password wasn't screaming 'please hack me'. Instead, the JDBC connection information should be in a file that doesn't get checked into the repository.
The password can be externalized by overriding the properties when instantiating the EntityManagerFactory:
private EntityManagerFactory getEntityManagerFactory() {
return Persistence.createEntityManagerFactory( getPersistenceUnitName(),
getProperties() );
}
private Map getProperties() {
Map result = new HashMap();
// Read the properties from a file instead of hard-coding it here.
// Or pass the password in from the command-line.
result.put( "javax.persistence.jdbc.password", "PASSWORD" );
return result;
}
I have embedded derby database and i work with jpa. This is my persistence.xml:
<?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="pers">
<class>entities.Leverancier</class>
<class>entities.Prijsproduct</class>
<class>entities.Product</class>
</persistence-unit>
</persistence>
What should i change or add to get this working. When I run my code now I get the following:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pers
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at test.Test.main(Test.java:19)
Your persistence.xml is not correct. Look at a sample below:
<persistence-unit name="MyAppPU" transaction-type="RESOURCE_LOCAL">
<!-- This is where you mention your JPA runtime provider e.g. it's EclipseLink here -->
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>mypkg.MyEntity</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/my_schema"/>
<property name="javax.persistence.jdbc.password" value="pass"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="user"/>
</properties>
</persistence-unit>
You also have to make sure that you put your JPA provider jar files (along with the derby-client jar) in the classpath.