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.
Related
I am creating a server in Glassfish and I want to have a JPA persistence layer to a remote MySQL database.
When I attempt to use the persistence layer, I get this exception: javax.persistence.PersistenceException: No Persistence provider for EntityManager named em1
Based on other StackOverflow posts, it seems this is an indication the persistence.xml is invalid. My persistence.xml is shown. Can someone point me in the right direction to figuring out how to get this to work?
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name ="em1">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>data.entry.Movie</class>
<properties>
<property name="eclipselink.target-database"
value="MySQL4"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="mysql3.cs.myschool.edu/mydb"/>
<property name="javax.persistence.jdbc.user" value="mydb" />
<property name="javax.persistence.jdbc.password" value="mypass" />
</properties>
</persistence-unit>
</persistence>
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;
}
Im trying to configure persistence.xml file which is needed for my EntityManagerFactory. I have created persistence file for MySQL DB a few months ago. But I have a little trouble with Oracle DB.
In MySQL connection I needed MySQL JDBC Connector. It is in Maven repository, so it was easy to fetch it and use. My problem is that I dont know which connector I have to use for connection to Oracle DB. Is that connector in Maven Repository?
Here is my present 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="oracleTest" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="javax.persistence.jdbc.user" value="library" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</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>
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.