Set value of persistence.xml file with properties.config file - java

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.

Related

exception JPA derby config for junit?

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

Persistence provider password predicament

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;
}

How Persistence Unit works in JPA

I am trying to learn JPA with Hibernate implementation. There are lot of blog of net about this but still i am struggling to implement it. I have written one Dao class which creates the EntityManagerFactory. But while looking this PERSISTENCE_UNIT it says
javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
private final String PERSISTENCE_UNIT = "test";
private EntityManager entityManager;
public GenericDao() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT);
entityManager = factory.createEntityManager();
}
I also created one persistence.xml file which is in META-INF folder of web application. It looks like this.
<?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">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgres://localhost:1532/test" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
For deployment Tomcat8 is being used. Please let me know why application not able to lookup PERSISTENCE_UNIT.
As per comment - the problem is your persistence.xml location. It needs to be exactly where your application expects it to be:
WEB-INF/classes/META-INF/persistence.xml
Now, why did you encounter this problem is another matter, connected with how you create your .war file. Solution that should work however, is trusting Maven to take care of that. If you set packaging to war, and put your META-INF folder into resources (src/main/resources/META-INF/persistence.xml) it should take care of things.

Don't know exactly what jta-data-source should be

I tried to get Spring-MVC, Hibernate, JTA with a Postgres Server to work.
I got most of the stuff working (Read from Db through EntityManager without JTA), but I can't get the transactions to work with JTA.
<?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="defaultPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>???</jta-data-source>
<class>net.test.test.database.UsersEntity</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="validpw"/>
</properties>
</persistence-unit>
If I provide my datasource from my servlet I get an Exception like
DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'dataSource'
I also found resources saying the datasource should be defined in the application-server (but I am not sure about this one). If it is important I use a Tomcat-server.

Can not find the declaration of element 'persistence'

Have put the persistence.xml in the classpath of the project in eclipse
because before the error was that the file was not found.
Now gives this error:
Caused by: javax.persistence.PersistenceException: Invalid
persistence.xml. Error parsing XML [line : -1, column : -1] :
cvc-elt.1: Can not find the declaration of element 'persistence'
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
xsi:schemalocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/Automoveis" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="1234" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
The problem is that you mix JPA 2.0 and JPA 2.1 notation.
Either this
<persistence 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"
version="2.1">
for JPA 2.1 or this
<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">
for JPA 2 but not a mix thereof.
See http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/index.html for details.
There is something slightly wrong with the XML provided, perhaps a missing version, perhaps the XML definition. Could also be a strange character or a typo somewhere.
A working template is below, try that instead.
<?xml version="1.0" encoding="UTF-8"?>
<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>
I have faced similar problem (Cannot find the declaration of element 'entity-mappings') in the past when I had persistence.xml with JPA version 2.0 & orm.xml file with version 2.1. I think the error reported above are similar.
Working samples for JPA 2. Read the sample below carefully and note their version. Ensure they are of same versions as in the samples. You may use JPA 2.1 and approprite schema reference as well.
persistence.xml
<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="SANJUSEJB" transaction-type="JTA">
<jta-data-source>jdbc/sanjusDataSourceXA</jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>org.sanjus.pa.ejb.entity.UserEntity</class>
</persistence-unit>
</persistence>
orm.xml
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd" version="2.0">
<named-query name="findUserJSONById">
<query>SELECT a.userJson FROM UserEntity a WHERE a.userId = :userId</query>
</named-query>
</entity-mappings>
Solved!
I do not know exactly what was wrong, but it worked well:
<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="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/Automoveis" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="1234" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>

Categories