JPA, EclipseLink, PostgreSQL and schemas - postgresql

I want to develop a Java EE application with JPA (EclipseLink implementation) and PostgreSQL as database.
I chose to have one database and multiples schemas instead of having multiples databases and one schema per database.
So, in my persistence.xml I have something like that :
<persistence-unit name="00" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>todo</jta-data-source>
<class>...</class>
<class>...</class>
<properties>
<property name="javax.persistence.target-database" value="PostgreSQL" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb;create=true" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="userpwd" />
</properties>
</persistence-unit>
<persistence-unit name="01" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>todo</jta-data-source>
<class>...</class>
<class>...</class>
<properties>
<property name="javax.persistence.target-database" value="PostgreSQL" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb;create=true" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="userpwd" />
</properties>
</persistence-unit>
I can easily reference persistence unit :
EntityManagerFactory emf = Persistence.createEntityManagerFactory("01");
EntityManager em = emf.createEntityManager();
but how can I store information about schemas in persistence.xml ? I want to access schema 00 or 01 in testdb database.
I probably could change schema via native SQL directly but is there a way to bind schema to persistence unit ?
Thank you

This same question was asked and answered here:
JPA - EclipseLink - How to change default schema
You can override the schema for the entire persistence unit using an orm.xml file, or define it within each table annotation or xml element as needed.

Related

JPA Query don't find EntityClass

Well, i'm trying to execute a simple query but i always got a "Not Mapped Entity". See my query :
"SELECT c FROM MyOwnClass c WHERE c.filter = :filter ORDER BY c.filter";
But if i put the qualified name of class "br.com.myproject.MyOwnClass" the query is executed.
This is my persistence.xml file:
<persistence-unit transaction-type="RESOURCE_LOCAL" name="myPU">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>java:jboss/datasources/myDS</non-jta-data-source>
<class>br.com.myproject.MyOwnClass</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="none"/>
<property name="hibernate.connection.characterEncoding" value="UTF-8"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
Are your entity classes in a different package than your query code? You probably just need to add an import:
import br.com.myproject.MyOwnClass;

Not generating schema in JPA using openjpa H2

I am trying to use an H2 in memory database to do some testing on my JPA entities. I am still stuck at the point of creating the schema. my persistance unit looks like this
<persistence-unit name="TEST_DS" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.domain.entities.ThisEntity</class>
<properties>
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="sa" />
<property name="openjpa.ConnectionURL" value="jdbc:h2:mem:test2" />
<property name="openjpa.ConnectionDriverName" value="org.h2.Driver"/>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true,schemaAction='dropDB,add')" />
</properties>
</persistence-unit>
I have tried various combinations of openjpa & javax.persistance properties without luck.
The exception I get is
org.apache.openjpa.persistence.PersistenceException: Schema "DBM" not found.
The schema exists in my entities

Integrate BoneCP and EclipseLink with LOCAL_RESOURCE transaction-type

I am setting up a minimal working example where I use EclipseLink in a Java EE (JSF) project. My example works perfectly with the following configuration and code:
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="issat_PU_Local" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>pro.entity.Utilisateur</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/issat" />
<property name="javax.persistence.jdbc.user" value="java" />
<property name="javax.persistence.jdbc.password" value="JaVa" />
<property name="eclipselink.logging.level.sql" value="FINE" />
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
Since I don't want to use JTA I am using RESOURCE_LOCAL as transaction type and perfectly leaving with "manually" handling transaction inside my DAO.
I now want to use BoneCP connection pool with my example.
How should I precisely modify my persistence.xml to do that? I found many configuration examples in the internet using also Hibernate, but I want to stay with EclipseLink.
I couldn't do it with bonecp, but I could get a pool connection with C3PO.
I followed this link : http://javabeginnerstutorial.com/hibernate/connection-pooling-with-hibernate-4/
I copied the dependency and the properties but, instead of <property name="hibernate.c3p0.min_size">1</property> put the value in the tag like this<property name="hibernate.c3p0.min_size" value="1"/>, otherwise I got an error.
Finally mi persitence.xml look this way:

Persistence.xml for JBoss AS 7.1.1

I have a client and server setup to run with JBoss AS 7.1.1 and I have a question about my persistence.xml. Currently it looks like this:
<persistence>
<persistence-unit name="GrahamsProj">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/WorkCenterDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>
I have persistence.xml in the META-INF folder of my server. I must be missing some properties though because I'm getting this error when I try to query the database I'm connected to.
11:35:07,840 ERROR [org.jboss.ejb3.invocation] (EJB default - 1) JBAS014134:
EJB Invocation failed on component GrahamsProjBean for method public abstract
void grahamsprojserver.session.interfaces.GrahamsProjBeanRemote.test(): javax.ejb.EJB
Exception: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:
Schemas is not mapped [FROM Schemas s]
Is there something I'm missing?
you need to include all your model classes in persistence.xml. example:
<persistence>
<persistence-unit name="GrahamsProj">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/WorkCenterDS</jta-data-source>
<class>your.class.model.Example</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>
You would probably need to update the hibernate.dialect property too. In your example you are using "org.hibernate.dialect.HSQLDialect" and you use Oracle drivers, e.g. "oracle.jdbc.OracleDriver". If you are using Oracle database, you need to update hibernate dialect, e.g. "org.hibernate.dialect.Oracle10gDialect".
Here's your updated code:
<persistence>
<persistence-unit name="GrahamsProj">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/WorkCenterDS</jta-data-source>
<class>your.class.model.Example</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>

I have just configured a new datasource on jboss

And created a new user, I have granted it select/update delete rights on specific tables as required.
Will this be enough to use persistence, or will jboss need admin rights as well ?
No, it shouldn't need admin rights.
Easiest answer - try it and see.
No, no need to assign admin rights. It will enough by using persistence.xml
please add following in your persistence.xml
<persistence-unit name="abc" transaction-type="JTA"> and
<jta-data-source>java:jboss/datasources/data_source_name</jta-data-source>
persistence.xml should like this:
<?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="abc" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/data_source_name</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- Map entity classes -->
<class> com.packege.classname </class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/DB_NAME" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
and provide the persistence unit name to createEntityManagerFactory method in Hibernate util class.
Persistence.createEntityManagerFactory("abc");