I'm Having a Weird problem with Spring Data.
I Have 2 different projects, witch one have your schema on postgres.
on the first project i can do the CRUD in any entity and everything work's fine.
And in the second project, i have dependency of the first project, i can do the CRUD in anything of the second project and list anything of the first project. But i can't persist anything of the first project
I Realize that if i throw this line
<import resource="classpath*:applicationContext.xml" />
in the end of my Application context of the second application, i CAN persist anything of the first project on the second project, BUT i CAN'T persist anymore the things of the second project.
And if i let this line in the begginning of the application-context.xml of the second aplication it will persist normal the model of the second projet.
here it is the complete XML Application-context of the second project:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Carregar os dados applicationOrcamento.properties na memória -->
<!-- <context:property-placeholder location="classpath*:applicationOrcamento.properties"
ignore-unresolvable="true"/> -->
<import resource="classpath*:applicationContext.xml" /> <!-- this line that i'm having problem Application Context of the first project if this line is here, i can persist anything of the second project, if i put on the end will persist any thing of the first project, but nothing of the second project... -->
<!-- Activates scanning of #Autowired -->
<context:annotation-config />
<context:component-scan
base-package="br.com.secondProject.package.repository" />
<context:component-scan base-package="br.com.secondProject.package.services" />
<!-- <context:component-scan base-package="br.com.firstProject.package.repository"
/> -->
<!-- <context:component-scan base-package="br.com.firstProject.package.services"
/> -->
<jpa:repositories base-package="br.com.secondProject.package.repository"
entity-manager-factory-ref="entityManagerFactoryOrcamento" />
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
<!-- Database -->
<bean id="datasourceOrcamento"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url"
value="jdbc:postgresql://localhost:5432/DB?searchpath=second_project" />
<property name="username" value="postgres" />
<property name="password" value="123456" />
</bean>
<!-- Entity Manager -->
<bean id="entityManagerFactoryOrcamento"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasourceOrcamento" />
<property name="persistenceUnitName" value="second-project-pu" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<!-- <property name="generateDdl" value="true" /> -->
</bean>
</property>
<property name="packagesToScan" value="br.com.secondProject.package.model" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<!-- Alterar para JTA quando for para produção! -->
<!-- <jee:jndi-lookup jndi-name="java:/prject-ds" id="datasource" resource-ref="true"/> -->
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryOrcamento" />
</bean>
<tx:annotation-driven />
`
Ther Persistence.xml of the second application is also importing the classes of the first project, th real problem is persist an entity of the first project on the second project
i have already tried to put on my controller the
#ContextHierarchy({
#ContextConfiguration(locations = { "/applicationContextOrcamento.xml"}), //second Project xml
#ContextConfiguration(locations = { "classpath*:applicationContext.xml" })// first project xml
})
but doens't work...
i just need to persist an entity of the first project inside the second project
Related
AuditingEntityListener correctly updates columns marked with #LastModifiedDate, #CreatedDate, #CreatedBy and #LastModifiedBy in dev mode (mvn jetty:run) when I use LocalContainerEntityManagerFactoryBean. However, when I activate prod profile and deploy in Wildfly 8, the columns are not updated.
I found on this forum post: "You will need to use one of Spring's EntityManagerFactoryBeans to setup the EntityManagerFactory" Is there any way to use AuditingEntityListener with <jee:jndi-lookup /> EntityManagerFactory?
Here is my applicationContext.xml
<beans profile="dev">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath:/META-INF/local-container-persistence.xml" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
<beans profile="prod">
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/postgresql-datasource" lookup-on-startup="false" expected-type="javax.sql.DataSource" />
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:jboss/entity-manager-factory" lookup-on-startup="false" expected-type="javax.persistence.EntityManagerFactory" />
<tx:jta-transaction-manager />
</beans>
<jpa:repositories base-package="com.corp.repository" factory-class="com.corp.RespositoryFactoryBean" />
<jpa:auditing auditor-aware-ref="entityAuditorAware" />
<bean name="entityAuditorAware" class="com.corp.EntityAuditorAware" />
orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
tl;dr
Make sure the EntityManagerFactory you obtain from JNDI is configured to have the AuditingEntityListener applied to the persistence unit you obtain.
Details
In your dev profile, The EntityManagerFactory is created within your application and thus - by definition in the spec - considers the locally available orm.xml.
In you prod example you don't bootstrap an EntityManagerFactory locally but obtain a preconfigured one from your application server. Thus you have to make sure the EntityManagerFactory instance is configured as you expected in the application server in the first place.
I am using Spring with Maven and Tomcat. I have a Spring MVC application which has a public front-end WAR,and a shared services JAR. My JAR contain list of service annotated by spring annotation #Service and i configure my Application context inside this jar like bellow.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.bergit.jpa" />
<!-- Import file which containt parameter need it to configure the DB and hibernate -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="META-INF/config/jdbc.properties" />
<!-- Database connection settings -->
<bean id="dataSource-seconde"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<!-- Configuration for Hibernate/JPA -->
<bean id="entityManagerFactory-seconde"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="dataSource" ref="dataSource-seconde" /> -->
<property name="persistenceUnitManager" ref="pum"/>
<property name="persistenceUnitName" value="testspring-jpa-seconde" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${jdbc.dialect}" />
</bean>
</property>
</bean>
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager-seconde"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory-seconde" />
<property name="dataSource" ref="dataSource-seconde" />
</bean>
</beans>
and this is my class java is writed like bellow:
#Service("userJARService")
public class UserJARServiceImpl implements UserJARService {...}
i tested my Jar using Junit and it's ok.
now i add this jar into my war (add inside the pom file of war).
i write my class of controller for my first view where i inject my first service which i want to use it inside this conctroller UserJARService like bellow:
#Controller
#RequestMapping(AccessClassToWebService.CONTROLLER_BASE_PATH)
public class AccessClassToWebService {
#Autowired
UserJARService userJARService;
}
now i add this jar dependency inside the pom of my war. But i get the error bellow
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.bergit.jpa.service.UserJARService ....
Hello I am trying to setup spring JTA in myEclipse for Spring.Below are my configuration files:
applicationContent.xml where i have added two imports(note it do include schema locations)
<import resource="infrastructure.xml"/>
<import resource="classpath:**/persistence.xml"/>
infrastrutre.xml(unable to add schema due to input validations)
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jtaDataSource" ref="masterDataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<!-- <bean id="masterDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/test_JTA"></property>
</bean> -->
<jee:jndi-lookup id="masterDataSource" jndi-name="java:/Test_JTA" />
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- <property name="databasePlatform"
value="org.hibernate.dialect.MySQL5InnoDBDialect" /> -->
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="mainPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.abc.PaymentCard</class>
<jar-file>mysql-connector-java-5.1.23-bin.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
<property key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
</properties>
</persistence-unit>
</persistence>
I am getting error
JBAS010402: Unable to instantiate driver class "com.mysql.jdbc.Driver": org.jboss.msc.service.DuplicateServiceException: Service jboss.jdbc-driver.JTA_Test_war is already registered
I goggled this error but still haven't got any satisfactory answers.Kindly help
I am not sure about JBoss but for Glassfish there is an Ext directory for the Jars you want in the Classpath. So try to figure out how to deploy mysql-connector-java*.jar to your domain.
i know its late but, i believe i fixed this error by using a different factory class.
<property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory"/>
hope it helps :)
hi my project is Hsql embedded with spring+hibernate
my diectory like
project name
..src
....domain
....dao
....service
....main
..applicationContext.xml
..db/database
my database name is database
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<!-- Component scan to find all Spring components -->
<context:component-scan base-package="com.habitz.librarymanagement" />
<!-- Data Source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value=" jdbc:hsqldb:db/database" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="initialSize" value="1" />
<property name="maxActive" value="5" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="10" />
</bean>
<!-- Hibernate Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- Hibernate configuration -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
<!-- The packages that contain our Hibernate model classes -->
<property name="packagesToScan">
<list>
<value>com.habitz.librarymanagement.domain</value>
</list>
</property>
<!--
<property name="cacheRegionFactory">
<bean id="cacheRegionFactory" class="org.hibernate.cache.impl.NoCachingRegionFactory" />
</property>
<property name="eventListeners">
<map></map>
</property>
-->
</bean>
<!-- Hibernate transaction management -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
when i run through main class it gives me
Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL ' jdbc:hsqldb:db/database'
canot find suitable drivers...
but hsqldb.jar in claspath..!!
someone know please help..!!
Regarding the previous answer to clarify confusion.
Both driver classnames are valid.
If you look at org.hsqldb.jdbcDriver it's a very simple class
/* Copyright (c) 2001-2011, The HSQL Development Group
package org.hsqldb;
import org.hsqldb.jdbc.JDBCDriver;
public class jdbcDriver extends JDBCDriver {}
All it does is extend org.hsqldb.jdbc.JDBCDriver and is therefore just an alias to it.
I'm not an expert of XML but it seems to me the name of the driver class is wrong.
Using plain Java code, I pass "org.hsqldb.jdbc.JDBCDriver" as the driver's classname.
I've got the following spring configuration (actually, the configuration is more extensive, but I've included relevant parts):
testApplicationContext.xml
<!-- Business -->
<import resource="contexts/testBusinessContext.xml" />
<!-- Dao -->
<import resource="contexts/testDaoContext.xml" />
<!-- Persistence configuration -->
<import resource="contexts/testPersistenceContext.xml" />
<!-- Service actions -->
<import resource="contexts/testServiceActionContext.xml" />
testBusinessContext.xml
<bean id="basketBusiness" class="com.company.salesdataservice.business.BasketBusiness">
<property name="basketDao" ref="basketDao" />
<property name="tokenDao" ref="tokenDao" />
<property name="houseDao" ref="houseDao" />
<property name="currencyDao" ref="currencyDao" />
</bean>
testDaoContext.xml
<bean id="currencyDao" class="com.company.salesdataservice.dao.CurrencyDao">
<property name="dataSource" ref="companyDomainDataSource" />
</bean>
<bean id="houseDao" class="com.company.salesdataservice.dao.HouseDao"/>
<bean id="basketDao" class="com.company.salesdataservice.dao.BasketDao">
<property name="dataSource" ref="companyBookingDataSource" />
</bean>
<bean id="tokenDao" class="com.company.salesdataservice.dao.JavaRandomTokenDao" />
testPersistenceContext.xml
<bean id="companyBookingTransactionManager" class="com.company.utils.data.TransactionManager">
<property name="manager" ref="companyBookingSpringTransactionManager" />
</bean>
<bean id="companyBookingSpringTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="companyBookingDataSource" />
</bean>
<!-- initialised by a DataSourceInitializer. Left out for brevity. -->
<jdbc:embedded-database id="companyBookingDataSource" type="H2"/>
<!-- initialised by a DataSourceInitializer. Left out for brevity. -->
<jdbc:embedded-database id="companyDomainDataSource" type="H2"/>
testServiceActionContext.xml
<bean id="createBasketServiceAction" class="com.company.salesdataservice.serviceaction.CreateBasketServiceAction">
<property name="transactionManager" ref="companyBookingTransactionManager" />
<property name="basketBusiness" ref="basketBusiness" />
</bean>
I'm trying to autowire a field of the type CreateBasketServiceAction in one of my tests:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/testApplicationContext.xml"})
public class CreateBasketServiceActionTest {
#Autowired
CreateBasketServiceAction createBasketServiceAction;
}
However, Spring keeps telling me that it can't autowire:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.company.utils.data.serviceaction.TransactionalServiceAction] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
If i replace that field with a BasketBusiness basketBusiness field, then Spring is perfectly capable of doing it.
As far as I can see, both CreateBasketServiceAction and BasketBusiness is defined as a <bean /> in the XML configuration.
I've been banging my head against the wall for the past few hours on this. What am I doing wrong?