How to passing Maven Profiles value to Spring Bean XML - maven

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

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"/>

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)

OpenJPA disabling create tables on startup

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.

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>

java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider exception while integrating spring and hiberate

I am getting the below exception when I am trying to test the spring and hibernate integration.
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 18 more
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"
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">
<context:annotation-config/>
<!-- Datasource beans -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/sterlingschema" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- Hibernate Template session factory bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>/org/droidaceapps/domain/users.hbm.xml</value>
</list>
</property>
</bean>
<!-- Hibernate template beans -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- DAO beans -->
<bean id="userDAO" class="org.droidaceapps.dao.UserDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<!-- Service beans -->
<bean id="userService" class="org.droidaceapps.services.UserServiceImpl">
<property name="userDAO" ref="userDAO" />
</bean>
When I googled on this problem some said that we should be using springframework.orm.hibernate4 instead of springframework.orm.hibernate3. I did that. But still I am getting the same exception.
Can someone help me in Identifying what am I missing here.
Thanks
Note : org.hibernate.cache.Provider was removed in the change from Hibernate 3 to Hibernate 4.
If you use hibernate 3, you may get the java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider.
If you change to hibernate 4, you will not get that, but you firstly should add hibernate4.jar and spring-orm.jar.
It sounds liken you are missing hibernate dependency in your project set up:
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider
Try downloading hibernate-core.jar and add to your project classpath.
If you use Maven, simply add the following dependency to your project's pom.xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
Hope that helps.
You are injecting HibernateTransactionManager into DAO class instead of HibernateTemplate. Please check your configuration your file.
This solution worked for me too:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.10.Final</version>
</dependency>
In my case, I already had the hibernate-entitymanager dependency, but was attempting to use the latest version of hibernate - 4.3.1.Final. Not sure why the downgrade was necessary here, but chances are the "Simple Spring JPA Utility Project" I was basing off in STS 3.5.0.M2 of is also a little dated?
You have to change in spring configuration file.
change :
HibernateTemplate class as "org.springframework.orm.hibernate3.HibernateTemplate"
,and change:
LocalSessionFactoryBean bean as "org.springframework.orm.hibernate3.LocalSessionFactoryBean"
it will work.
This might be because you are using "org.springframework.orm.hibernate3.LocalSessionFactoryBean" instead of "org.springframework.orm.hibernate4.LocalSessionFactoryBean" for "sessionFactory" in spring configuration file.
If you're using Maven, try putting this in your POM file:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.10.Final</version>
</dependency>
That worked for me.

Resources