apache derby + jpa - java

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.

Related

Eclipselink JPA NoSQL No Persistence Provider

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

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

WAS Liberty 8.5.5.4, JPA 2.0 - discovered implementations: NONE

I was able to get JPA working with eclipse Luna and installing eclipseLink (and created a User Lib with the 3 jars).
However, I have now moved to Rational Application Developer (RAD) and attempting to use the JPA that is delivered with WAS Liberty. I initially used RAD JPA facet to configure JPA and then edited persistence.xml.
I am getting this error:
Caused by: javax.persistence.PersistenceException: No persistence providers available for "test2" after trying the following discovered implementations: NONE
I assume (always dangerous) the NONE is a configuration error and I cannot do anything until that is resolved...
I have copied the wlp\dev\api\spec\com.ibm.ws.javaee.persistence.2.0_1.0.1.jar into WEB-INF/lib and included that in my java build path.
As you can see below, I was guessing my provider was not correct, and tried several values...
<?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="test2" transaction-type="RESOURCE_LOCAL">
<!-- provider>org.eclipse.persistence.jpa.PersistenceProvider</provider-->
<!-- provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider-->
<provider>com.ibm.websphere.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>java:comp/env/New Generic JDBC</non-jta-data-source>
<class>com.ibm.analytics.jpa.Dimtask</class>
<properties>
<property name="openjpa.jdbc.Schema" value="RM"/>
<property name="javax.persistence.jdbc.url" value="jdbc:netezza://netezza-1.site.ibm.com:5480/DATABASENAME"/>
<property name="javax.persistence.jdbc.user" value="jspoon"/>
<property name="javax.persistence.jdbc.password" value="xxxxxxx"/>
<property name="javax.persistence.jdbc.driver" value="org.netezza.Driver"/>
</properties>
</persistence-unit>
</persistence>
What else can I do to get past the NONE?
Here is the updated version of persistence.xml, I just commented out the provider.
<?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="test2" transaction-type="RESOURCE_LOCAL">
<!-- provider>org.eclipse.persistence.jpa.PersistenceProvider</provider-->
<!-- provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider-->
<!-- provider>com.ibm.websphere.persistence.PersistenceProviderImpl</provider-->
<non-jta-data-source>java:comp/env/New Generic JDBC</non-jta-data-source>
<class>com.ibm.analytics.jpa.Dimtask</class>
<properties>
<property name="openjpa.jdbc.Schema" value="RM"/>
<property name="javax.persistence.jdbc.url" value="jdbc:netezza://netezza-1.boulder.ibm.com:5480/BACC_TST_ISHANGO_DW"/>
<property name="javax.persistence.jdbc.user" value="jspoon"/>
<property name="javax.persistence.jdbc.password" value="C0ke4y0u"/>
<property name="javax.persistence.jdbc.driver" value="org.netezza.Driver"/>
</properties>
</persistence-unit>
</persistence>
I created my Entity class using JPA Tooling, and using a simple junit test to call a named query. The first step is to create the EMF and it is a static in my class (so none of the test is actually running, it is failing at instantiation creating the static variable):
static EntityManagerFactory emf = Persistence.createEntityManagerFactory( "test2" );
This is the exact same code as used before with eclipse and eclipseLink.

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.

How to create database tables from Java code using JPA and JavaDB embedded?

My application uses JPA and JavaDB in embedded mode. In the persistence.xml file I use this property:
<property name="javax.persistence.jdbc.url" value="jdbc:derby:meuspiladb;create=true" />
so an empty database will be created if it does not exist yet.
Currently I have a create_tables.sql file where I put the SQL code to create the tables, and then I run it manually after the database is created. I would like to make this be automatic but I don't know how.
If the database is new I have to create the tables. What is the best way to create them from Java code?
My persistence.xml file:
<?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="MeusPila3_PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>br.meuspila.corretora.Corretora</class>
<class>br.meuspila.ativo.Ativo</class>
<class>br.meuspila.bolsa.Bolsa</class>
<class>br.meuspila.movimento.Movimento</class>
<class>br.meuspila.provento.Provento</class>
<class>br.meuspila.operacao.Operacao</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:meuspiladb;create=true" />
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
</properties>
</persistence-unit>
</persistence>
You can use Hibernate with JPA, then use configuration option hibernate.hbm2ddl.auto=create.

Categories