OpenJPA disabling create tables on startup - spring

I must use an external database in a spring non web application.
How can i disable openjpa to try to create entity tables? My entities EmailAddress and Message has annotations "#Entity", i do not know if it is right.
<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>pack.EmailAddress</class>
<class>pack.Message</class>
<properties>
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/kepsDb" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="openjpa.jdbc.SynchronizeMappings" value="false"/>
</properties>
</persistence-unit>

Remove the openjpa.jdbc.SynchronizeMappings property for your persistence.xml file.

Related

Using JPA to connect oracle database,but it still shows Could not load requested class : oracle.jdbc.driver.OracleDriver

I use JPA configuration file,persistence.xml,to set the connection info below:
<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">
<persistence-unit name="mydb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.test.vo.Customer</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin://localhost:1521/orcl" />
<property name="javax.persistence.jdbc.user" value="cuser" />
<property name="javax.persistence.jdbc.password" value="cuser" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
And I have added ojdbc8.jar in Maven repository:
https://i.imgur.com/FGIlQxg.png
And I import ojdbc8.jar in Maven dependecy:
https://i.imgur.com/bkXg65L.png
And I also set ojdbc8.jar info in pom.xml:
https://i.imgur.com/koleE8Y.png
But when I try to connect to oracle database,it just shows
java.lang.ClassNotFoundException: Could not load requested class : oracle.jdbc.driver.OracleDriver
and I see the ojdbc8.jar in Maven dependecy ,the ojdbc8.jar surely contains oracle.jdbc.driver.OracleDriver
What happened about this?HOW Can I do to fix it?
Try Using
oracle.jdbc.OracleDriver
instead of
oracle.jdbc.driver.OracleDriver
This is what worked for me. Check your URL, which is not correct.
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.user" value="username"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.url" value=""jdbc:oracle:thin:#myhost:1521/myorcldbservicename"/>

How to passing Maven Profiles value to Spring Bean XML

I'm new in Spring & Maven, I think my question is simple. But I cannot to figure and setup it. I have Maven POM like below:
<profiles>
<profile>
<id>qa</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<db.driverClassName>oracle.jdbc.driver.OracleDriver</db.driverClassName>
<db.url>jdbc:oracle:thin:#10.148.36.89:1521:mmki</db.url>
<db.username>APW</db.username>
<db.password>apw</db.password>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<db.driverClassName>oracle.jdbc.driver.OracleDriver</db.driverClassName>
<db.url>jdbc:oracle:thin:#10.148.36.88:1521:mmki</db.url>
<db.username>APW</db.username>
<db.password>apw</db.password>
</properties>
</profile>
</profiles>
My question is how I can passing the value from Maven profile to Spring bean property like below:
<!-- QA ENVIRONMENT -->
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#10.148.36.89:1521:mmki" />
<property name="username" value="APW" />
<property name="password" value="apw" />
</bean> -->
<!-- PRD ENVIRONMENT -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#10.148.36.88:1521:mmki" />
<property name="username" value="APW" />
<property name="password" value="apw" />
</bean>
I'm so dumb for this question, but please everyone to answer and explain with the simple method.
Many thanks.
First:
Do not write user and pass at pom.xml file
Second:
You can have one applicationContext for each environment like applicationContext-prod.xml and applicationContext-qa.xml
On your pom.xml you can have one property for each profile like:
pom.xml
<profile>
<id>qa</id>
<properties>
<appContext>classpath:applicationContext-qa.xml</appContext>
...
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<appContext>classpath:applicationContext-prod.xml</appContext>
...
</properties>
</profile>
In web.xml you can write you context param like:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>${appContext}</param-value>
</context-param>
If you write your passwords in the applicationContext files you do not need anything else. But, for security reasons I recommend you as best practice write your sensible environment values at external properties file:
You can have multiple *.properties on /etc/app
(production.properties and qa.properties)
And finally, on your applicationContext-prod.xml you can have your propertyConfigurer like:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:/etc/app/production.properties</value>
</property>
</bean>
....
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
....
<property name="password">
<value>${db.password}</value>
And on your applicationContext-qa.xml you can have your propertyConfigurer like:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:/etc/app/qa.properties</value>
</property>
</bean>
...
Obviously on your /etc/app/production.properties you have to write:
db.password=prodpass
Finally, on your /etc/app/qa.properties you have to write:
db.password=qapass

Setting JDBC connection/socket timeout via persistence.xml

I'm trying to set JDBC timeout via persistence.xml, but it doesn't work.
This is how my persistence.xml looks like:
<persistence-unit name="persistenceUnit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>OracleDataSourceExample</jta-data-source>
<properties>
<property name="oracle.net.READ_TIMEOUT" value="1"/>
<property name="oracle.jdbc.ReadTimeout" value="1"/>
<property name="oracle.net.CONNECT_TIMEOUT" value="1"/>
</properties>
</persistence-unit>
So, does anyone have suggestion what to do? (how to set jdbc timeout)

Accessing a datasource from an OSGi bundle

I have a blueprint file containing a datasource deployed to Apache ServiceMix. I was able to query the datasource from Apache Karaf console. How can I access this datasource from a Camel Spring-DM bundle application? This is my blueprint file:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="URL"/>
<property name="user" value="USER"/>
<property name="password" value="PASSWORD"/>
</bean>
<service interface="javax.sql.DataSource" ref="dataSource" id="ds">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/ds"/>
</service-properties>
</service>
</blueprint>
You can bind the DataSource as an OSGi service. In spring dm this is osgi:reference, in blueprint it would be reference.
<reference id="dataSource" interface="javax.sql.DataSource"/>
You can then inject the DataSource for example into the SqlComponent.
As an example see a fix I did for this camel route. This is blueprint but it is almost the same for spring dm.
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource"/>
</bean>
Using Hibernate as JPA provide:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="jpa" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>osgi:service/jdbc/ds</jta-data-source>
...
</persistence-unit>
</persistence>

Spring and Hibernate - changing dialect

In our web app that uses Spring and Hibernate, the hibernate configuration is in the META-INF/persistence.xml, but there is one problem, we are using two different databases, one for testing and other one for production.
Here is our `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="SpringMVCTest" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/comp/env/jdbc/sqliteDS</jta-data-source>
<class>pl.meble.taboret.model.UserEntity</class>
<class>pl.meble.taboret.model.WordList</class>
<class>pl.meble.taboret.model.WordUnit</class>
<class>pl.meble.taboret.model.ActivateUserAccountPermaLink</class>
<class>pl.meble.taboret.model.ResetPasswordPermaLink</class>
<class>pl.meble.taboret.question.QuestionUnit</class>
<class>pl.meble.taboret.question.OpenQuestion</class>
<class>pl.meble.taboret.question.MultipleChoiceQuestion</class>
<class>pl.meble.taboret.question.WithGapsQuestion</class>
<class>pl.meble.taboret.question.QuestionList</class>
<class>pl.meble.taboret.answer.AnswerUnit</class>
<class>pl.meble.taboret.answer.OpenQuestionAnswer</class>
<class>pl.meble.taboret.answer.MultipleChoiceQuestionAnswer</class>
<class>pl.meble.taboret.answer.WithGapsQuestionAnswer</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="pl.meble.taboret.utils.SQLiteDialect" />
<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.connection.driver_class" value="${database.driver}"
/> <property name="hibernate.connection.url" value="${database.url}" /> -->
<!-- <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/> -->
<!--<property name="hibernate.transaction.factory_class" value="com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory"/> -->
<property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>
so, is it possible to change the value of hibernate.properties at runtime, or store this value for example in JNDI resource?
Or is there some other way to conditionally set hibernate.dialect, so for example for testing we would have SQLite dialect and for normal deploy he would use Postgre dialect.
Yes. In spring you define the entity manager with a bean:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
You can configure properties of that bean:
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
where ${hibernate.dialect} is spring property. So you can pass the property when starting your project (either via -Dhibernate.dialect, or by placing it in a properties file and loading it with <context:property-placeholder-configurer>

Resources