Configuring Hibernate persistence.xml with Oracle Database - java

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>

Related

javax.persistence.PersistenceException: No Persistence provider for EntityManager named em1

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>

Unable to resolve mapping file using Spring and JPA

I'm trying to integrate Spring with JPA. I have defined one entity and added as mapping file under persistence.xml, but it throws the exception
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MyPersistence] Unable to resolve named mapping-file [net.bb.spring.entities.DealDetailsEntity]
My 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="MyPersistence">
<description>AS400 Configuration for BB</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>net.bb.spring.entities.DealDetailsEntity</mapping-file>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.ibm.as400.access.AS400JDBCDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:as400://172.21.100.101/BB" />
<property name="javax.persistence.jdbc.user" value="ROOT" />
<property name="javax.persistence.jdbc.password" value="ROOT" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
From oracle docs about persistence.xml file:
<mapping-file> element: specifies one or more object-relational
mapping XML files (orm.xml files).
What your <mapping-file> is pointing at is an entity which is a java class. This should not pass to <mapping-file> but to a <class> tag instead.
e.g.
<class>net.bb.spring.entities.DealDetailsEntity</class>

EclipseLink SessionCustomizer not getting called

I am using JPA with EclipseLink for PostGreSQL. Due to some product requirement I want to switch the schema runtime. I am planning to do it in "customize" method of SessionCustomizer as shown below
#Override
public void customize(Session session) throws Exception {
System.out.println("calling customize ###################");
if (this.schemaName != null || !this.schemaName.isEmpty()) {
session.getLogin().setTableQualifier(schemaName);
}
I set MySessionCustomizer in peristence.xml.
But don't know when and who will call MySessionCustomizer's customize method.
Please help me to understand this.
EDIT:
Here is my persistence.xml
<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" >
<class>com.demo.Student</class>
<properties>
<property name="eclipselink.session.customizer" value="com.demo.MySessionCustomizer"/>
<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="username" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
</persistence>
I am able to connect to the database using this persistence.xml and read/write from public schema of the database. However, I want to read and write from different schema and I also want to be able to change the schema at runtime.
I tried using "?currentSchema=MYSCHEMA" in DB URL, but it didn't work.

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.

EclispseLink + MongoDB parallel transactions

Running very simple example in a multi-threading java 7 application:
em.getTransaction().begin();
SimpleObject simpleObject = ...;
em.persist(simpleObject);
em.getTransaction().commit();
causes following error:
java.lang.IllegalStateException: Exception Description: Transaction is currently active
Running in a one thread all is fine.
My persistence.xml settings:
<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="mongo" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>test\SimpleObject</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<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.host" value="localhost" />
<property name="eclipselink.nosql.property.mongo.port" value="27017" />
<property name="eclipselink.nosql.property.mongo.db" value="test" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.nosql.property.mongo.write-concern" value="MAJORITY" />
<property name="eclipselink.jpa.uppercase-column-names" value="true" />
</properties>
</persistence-unit>
</persistence>
Version of used libraries:
EclipseLink 2.5.2,
mongoDB 2.6.1,
jpa 2.1.0,
mongo-java-driver-2.12.2,
eclipse.persistence.nosql-2.5.1
Is there anything wrong?
Thank you!

Categories