openJPA's persistence.xml configuration - openjpa

Earlier in my project I was using a combination of Hibernate 3.3.2, openJPA 2.1.1 to connect to the database and retrieve the information from table. Now I want to remove Hibernate and use openJPA for doing connection and retrieving the information.
My earlier configuration of persistence.xml was
<?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 persistence_2_0.xsd">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.connection.url" value="jdbc:mysql://10.10.10.10:3306/test?autoReconnect=true"/>
<property name="hibernate.connection.username" value="user"/>
<property name="hibernate.connection.password" value="pwd"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.OSCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="1800"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="1800"/>
<property name="c3p0.idleConnectionTestPeriod" value="1810"/>
</properties>
</persistence-unit>
</persistence>
Now I want to move to openJPA 2.1.1 and for that my persistence.xml file is
<?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="TestOpenJPAPersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://10.10.10.10:3306/test?autoReconnect=true"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="pwd"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionFactoryProperties" value="MaxActive=10,MaxIdle=5,MinIdle=2,MaxWait=1800000"/>
<property name="openjpa.Log" value="File=E:\\temp\\TestOpenJPAPersistence\\org.apache.openjpa.log, DefaultLevel=DEBUG, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.MySQLDictionary"/>
<property name="openjpa.DataCache" value="true"/>
<property name="openjpa.QueryCache" value="true"/>
</properties>
</persistence-unit>
</persistence>
My question is what are the replacement property for following hibernate property in openJPA
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.OSCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="1800"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="1800"/>

After many trial and error I have found some replacement parameters. My present persistence.xml file looks like
persistence.xml:
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://xx.xx.xx.xx:3306/dbname"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName" value="xxx"/>
<property name="openjpa.ConnectionPassword" value="xxx"/>
<property name="openjpa.DynamicEnhancementAgent" value="true"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
<property name="openjpa.Log" value="SQL=TRACE"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true, MaxActive=10, MaxIdle=5, MinIdle=2, MaxWait=60000"/>
</properties>
For connection pooling information check and for all OpenJPA Properties check
Hope this helps

Related

wildfly not use persistence-unit config

i use this configuration in persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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_2.xsd"
version="2.2">
<persistence-unit name="xxxxx" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.behpanel.demo.model.Sessions</class>
<class>com.behpanel.demo.model.Invoices</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/qqqqq"/>
<property name="javax.persistence.jdbc.user" value="****"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="******"/>
<property name="dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.physical_naming_strategy"
value="com.behpanel.demo.config.NamingStrategy"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="show_sql" value="true"/>
<property name="format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
when run application in intellij idea and deploy on local wildfly server. wildfly dont use this persistence-unit and use default data source.
and crud actions not persist on mysql db and that actions persist on default datasource that is h2 database.

Hibernate - Postgresql properties access

I am not able to access the postgresql db properties, which are in small letters. I would like to access the properties using camelCase. Please suggest me the required configuration .
for configure postgreSQL using JPA you can use this persistence.xml
For that you have to create one folder called "META-INF" in your project. and in that folder you have to store this file.
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="give_some_name" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/database_name" />
<property name="javax.persistence.jdbc.user" value="username" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Or using hibernate.cfg.xml
(Here no need to create "META-INF folder")
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/database_name</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>

How do I properly setup SQL logging for EclipseLink?

I want to see the generated SQL statements from EclipseLink. The persistence.xml contains the following properties:
<properties>
<property name="eclipselink.logging.file" value="/workspace/logs/JPA.log" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.logging.level.sql" value="ALL" />
<property name="eclipselink.logging.logger" value="JavaLogger"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mariadb://192.168.178.42:3306/halsol" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="pw" />
<property name="jdbc.persistence.logging" value="true" />
<property name="jdbc.persistence.logfile" value="/workspace/logs/JDBC.log" />
</properties>
The file "JPA.log" gets generated (as well as JDBC.log) but is has no contents after loading some rows from the database. I'm using EclipseLink 2.5.
I tried the same properties you have, it should work if you remove the following line:
<property name="eclipselink.logging.logger" value="JavaLogger"/>
The eclipselink.logging.logger parameter is provided for overriding default eclipselink logger.
Try either not setting this property or setting up java.util.logging within your application.

JPA Change default Flushmode from AUTO to COMMIT

I've tried several ways to change the FlushMode to the complete application.
Is this right or is there another way to do it?
I don't want to do this pragmmatically.
This is was what i find as property but it isn't work.
<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="myPU">
<properties>
...
<property name="hibernate.connection.autocommit" value="false"/>
<!-- Also tried this: -->
<property name="org.hibernate.FlushMode" value="commit"/>
...
</properties>
</persistence-unit>
</persistence>
Update:
I've created the class as described in the link by zxcf, but i can't figure out how to add this construct in my persistence.xml.
<property name="jpaDialect">
<bean class="test.jpa.vendor.HibernateJpaDialect">
<property name="flushMode" value="MANUAL"/>
</bean>
</property>
Try this
<property name="org.hibernate.flushMode" value="COMMIT"/>
Testing this on a standalone program I can see the changed value of underlying Hibernate Session / EntityManager from AUTO to COMMIT
Here is my persistence.xml
<?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-unit name="JPATest" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.test.TestEntity</class>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="xxx"/>
<property name="hibernate.connection.password" value="xxx"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
<property name="hibernate.cache.provider_configuration" value="classpath:ehcache.xml"></property>
<property name="hibernate.use.second.level.cache" value="true"/>
<property name="hibernate.cache.region_prefix" value="neutrino.jpa.cache"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.order_updates" value="true"/>
<property name="hibernate.order_inserts" value="true"/>
<property name="org.hibernate.flushMode" value="COMMIT"/>
</persistence-unit>
</persistence>
And here is how I test it
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPATest");
EntityManager em = emf.createEntityManager();
Session session = em.unwrap(Session.class);
System.out.println("Underlying Hibernate session flushmode ####### "+session.getFlushMode());
System.out.println("EntityManager flushmode ####### "+em.getFlushMode());
This gives me
Underlying Hibernate session flushmode ####### COMMIT
EntityManager flushmode ####### COMMIT
If I omit the property in presistence.xml, I get this
Underlying Hibernate session flushmode ####### AUTO
EntityManager flushmode ####### AUTO
Ancient Hibernate 3.2.4.sp1 from JBoss 4.2.3.GA supports only the following notation:
<property name="flush-mode" value="commit"/>

Using Spring data JPA with MS SQL server

I am setting up an app using Spring data JPA, Hibernate and MS SQL Server and unfortunately, I got a beat messed up with the configurations.
I hope someone here code make things clearer:.
This is my mvc-dispatcher.xml which is the application context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:component-scan base-package="com.yyy.yyy" />
<context:property-placeholder location="classpath:db.properties"/>
<!-- DATA BASE -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="database" value="SQL_SERVER" />
</bean>
</property>
<property name="persistenceUnitName" value="punit" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pwd}" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jpa:repositories base-package="com.yyy.yyy.yyy.repository"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Now, I also have a 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="punit">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.connection.url" value="${jdbc.url}" />
<property name="hibernate.connection.driver_class" value="${jdbc.driverClass}" />
<property name="hibernate.connection.username" value="${jdbc.user}" />
<property name="hibernate.connection.password" value="${jdbc.pwd}" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
I don't understand where should I put the DB connection data?
And I also cant connect to the DB (I get connection refused) - I am getting exception at server startup.
My project runs on Tomcat and I used https://github.com/SpringSource/spring-data-jpa-examples as a template.
EDIT
I managed to overcome the problems and publish to tomcat successfully,by removing the persistence XML file and use only the spring context file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="yyy.yyy.yyy" />
<context:property-placeholder location="classpath:db.properties" />
<!-- DATA BASE -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="yyy.yyy.yyy.yyy.domain" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="showSql" value="true" />
<property name="database" value="SQL_SERVER" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://localhost;databaseName=db" />
<property name="username" value="yyy" />
<property name="password" value="yyyyy" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="yyy.yyy.yyy.yyy.repository" />
<!-- MVC -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
The app now loads fine, but when I use my repository and call save, I get connection refused exception.
Anyone knows why?
Help please.
Idob
You are refering to db.properites file here:<context:property-placeholder location="classpath:db.properties"/>
Where is it located in the project?