Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Not using JNDI but get JNDI lookup failed error!

  1. #11

    Default

    Quote Originally Posted by Marten Deinum View Post
    Can you post your full configuration and your dao...
    Persistence.xml
    HTML Code:
    <?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_1_0.xsd"
    	version="1.0">
    
    	<persistence-unit name="milins-domain"
    		transaction-type="RESOURCE_LOCAL">
    		<provider>
                org.eclipse.persistence.jpa.PersistenceProvider
            </provider>
    <!--        <jta-data-source>jdbc/milinsDataSource</jta-data-source>-->
    		<class>myPackage.myClass1</class>
    		<class>myPackage.myClass2</class>
    		<class>myPackage.myClass3</class>
    	</persistence-unit>
    	
    	<persistence-unit name="levfr-domain"
    		transaction-type="RESOURCE_LOCAL">
    		<provider>
                org.eclipse.persistence.jpa.PersistenceProvider
            </provider>
    		<class>myPackage.myClass4</class>
    	</persistence-unit>	
    
    </persistence>
    Spring-master.xml
    HTML Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    	<bean
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<!-- <value>classpath:projectsettings.properties</value> -->
    				<value>file:${user.home}/milins.properties</value>
    				<value>file:${user.home}/milins.test.properties</value>
    			</list>
    		</property>
    		<property name="ignoreResourceNotFound" value="false" />
    	</bean>
    	<context:component-scan base-package="se.lantmateriet.milins">
    <!--		<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>-->
    		<context:exclude-filter expression="org.springframework.stereotype.Repository" type="annotation"/>
    	</context:component-scan>
    	
        <!-- This will ensure that hibernate or jpa exceptions are automatically translated into
             Spring's generic DataAccessException hierarchy -->
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    	
    	<import resource="spring-datasource.xml"/>
    	
    	<!--We should import spring-jpa.xml if we would like to use JPA (EntityManager)-->
    	<import resource="spring-eclipselink.xml" />
    </beans>
    Spring-datasource.xml
    HTML Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee"
    	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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
    
    	 <bean id="milinsDataSource" 
    	 class="org.springframework.jdbc.datasource.SingleConnectionDataSource" 
    	 p:driverClassName="${jdbc.driverclassname}" 
    	 p:url="${milins.jdbc.url}" 
    	 p:suppressClose="true" 
    	 p:username="${milins.jdbc.username}" 
    	 p:password="${milins.jdbc.password}"/> 
    
    <!--This setting should be used when we will start using JBoss (an AS)-->
    <!--	<bean id="milinsDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"-->
    <!--		p:jndiName="jdbc/milinsDataSource" p:resourceRef="true" />-->
    
    
    	 <bean id="levfrDataSource" 
    	 autowire-candidate="false" 
    	 class="org.springframework.jdbc.datasource.SingleConnectionDataSource" 
    	 p:driverClassName="${jdbc.driverclassname}" 
    	 p:url="${levfr.jdbc.url}" 
    	 p:suppressClose="true" 
    	 p:username="${levfr.jdbc.username}" 
    	 p:password="${levfr.jdbc.password}"/> 
    
    <!--	<bean id="levfrDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"-->
    <!--		p:jndiName="jdbc/levfrDataSource" p:resourceRef="true" />-->
    
    </beans>
    Spring.eclipselink.xml
    HTML Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:aop="http://www.springframework.org/schema/aop" 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
                               http://www.springframework.org/schema/tx
                               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    	<bean id="jpaVendorAdapter"
    		class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"
    		p:databasePlatform="org.eclipse.persistence.platform.database.OraclePlatform"
    		p:generateDdl="false" p:showSql="true" />
    
    	<!-- Milins Hibernate EntityManagerFactory -->
    	<bean id="milinsEntityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    		p:persistenceXmlLocation="classpath*:META-INF/persistence.xml"
    		p:dataSource-ref="milinsDataSource" p:jpaVendorAdapter-ref="jpaVendorAdapter"
    		p:persistenceUnitName="milins-domain">
    
    		<property name="jpaDialect">
    			<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    		</property>
    		<property name="jpaProperties">
    			<props>
    				<prop key="eclipselink.weaving">false</prop><!-- <prop key="eclipselink.ddl-generation">drop-and-create-tables</prop> 
    					<prop key="eclipselink.ddl-generation.output-mode">sql-script</prop> -->
    				<prop key="eclipselink.application-location">src/test/resources/</prop>
    				<!-- Make us participate in JBoss JTA transactions -->
    				<!-- <prop key="eclipselink.target-server">org.eclipse.persistence.platform.server.jboss.JBossPlatform</prop> -->
    			</props>
    		</property>
    	</bean>
    
    	<bean id="milinsEntityManager"
    		class="org.springframework.orm.jpa.support.SharedEntityManagerBean"
    		p:entityManagerFactory-ref="milinsEntityManagerFactory" />
    
    	<bean id="milinsTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    		p:entityManagerFactory-ref="milinsEntityManagerFactory" />
    
    	<!-- LevFR Hibernate SessionFactory -->
    	<bean id="levfrEntityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    		p:persistenceXmlLocation="classpath*:META-INF/persistence.xml"
    		p:dataSource-ref="levfrDataSource" p:jpaVendorAdapter-ref="jpaVendorAdapter"
    		p:persistenceUnitName="levfr-domain">
    
    		<property name="jpaDialect">
    			<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    		</property>
    		<property name="jpaProperties">
    			<props>
    				<prop key="eclipselink.weaving">false</prop><!-- <prop key="eclipselink.ddl-generation">drop-and-create-tables</prop> 
    					<prop key="eclipselink.ddl-generation.output-mode">sql-script</prop> -->
    				<prop key="eclipselink.application-location">src/test/resources/</prop>
    				<!-- Make us participate in JBoss JTA transactions -->
    				<!-- <prop key="eclipselink.target-server">org.eclipse.persistence.platform.server.jboss.JBossPlatform</prop> -->
    			</props>
    		</property>
    	</bean>
    
    	<bean id="levfrEntityManager"
    		class="org.springframework.orm.jpa.support.SharedEntityManagerBean"
    		p:entityManagerFactory-ref="levFREntityManagerFactory" />
    
    	<bean id="levfrTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    		p:entityManagerFactory-ref="levFREntityManagerFactory" />
    
    	<!-- Read in DAOs from the eclipselink package -->
    	<context:component-scan base-package="se.lantmateriet.milins.dao.eclipselink" />
    
    	<!-- Enable @Repository, @PersistenceContext support -->
    	<bean
    		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    	<!-- Enable @Transactional support -->
    	<tx:annotation-driven />
    
    </beans>

    one of my dao in next post....
    Last edited by Sheri; Jan 13th, 2011 at 01:07 AM.

  2. #12

    Default

    Quote Originally Posted by Marten Deinum View Post
    Can you post your full configuration and your dao...
    And my DAOs

    My GenericDao
    Code:
    package se.lantmateriet.milins.dao.eclipselink;
    
    import java.lang.reflect.ParameterizedType;
    
    import javax.annotation.Resource;
    import javax.persistence.EntityManager;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.transaction.annotation.Transactional;
    
    import se.lantmateriet.milins.dao.GenericDao;
    import se.lantmateriet.milins.domain.AbstractDomainMigrationInfo;
    
    public class GenericDaoEclipseLink<T extends AbstractDomainMigrationInfo>
    		implements GenericDao<T> {
    
    	private Class<T> type;
    
    	@Autowired
    	@Resource(mappedName = "milinsEntityManager")
    	protected EntityManager entityManager;
    
    	@Override
    	public void setEntityManager(final EntityManager entityManager) {
    		this.entityManager = entityManager;
    	}
    
    	@SuppressWarnings("unchecked")
    	public GenericDaoEclipseLink() {
    		this.type = (Class<T>) ((ParameterizedType) this.getClass()
    				.getGenericSuperclass()).getActualTypeArguments()[0];
    	}
    
    	//
    	//
    	// public GenericDaoEclipseLink(Class<T> type) {
    	// super();
    	// this.type = type;
    	// }
    
    	@Override
    	@Transactional
    	public T create(final T o) {
    		this.entityManager.persist(o);
    		return o;
    	}
    
    	@Override
    	@Transactional
    	public void save(final T o) {
    		T object = this.fetch(o.getId());
    		if (object == null) {
    			// Object does not exists, so safe to merge..
    			// if the object does not exist, merge will create new entity in db
    			this.entityManager.merge(o);
    		}
    	}
    
    	@Override
    	@Transactional
    	public void delete(final T o) {
    		this.entityManager.remove(this.fetch(o.getId()));
    	}
    
    	@Override
    	@Transactional
    	public T fetch(final Long id) {
    		return this.entityManager.find(this.type, id);
    	}
    }
    One of my Dao
    Code:
    package se.lantmateriet.milins.dao.eclipselink;
    
    import java.util.List;
    
    import org.springframework.dao.DataAccessException;
    import org.springframework.stereotype.Repository;
    
    import se.lantmateriet.milins.dao.IrFastighetDao;
    import se.lantmateriet.milins.domain.inskrivning.IrFastighet;
    
    @Repository("irFastighetDao")
    @SuppressWarnings("unchecked")
    public class IrFastighetDaoEclipseLink extends GenericDaoEclipseLink<IrFastighet>
    		implements IrFastighetDao {
    
    	@Override
    	public List<IrFastighet> getIrFastighetByFnr(String fnr)
    			throws DataAccessException {
    		List<IrFastighet> lists = this.entityManager
    				.createQuery(
    						"select irFastigheter from IrFastighet irFastigheter where irFastigheter.fnr = :fnr")
    				.setParameter("fnr", fnr).getResultList();
    		// if (lists == null || lists.size() <= 0)
    		// throw new EntityNotFoundException("Ingen IrFastighet med fnr = "
    		// + fnr + " finns!");
    		return lists;
    	}
    
    	@Override
    	public IrFastighet getIrFastighetByFnrAndVersion(String fnr, int version) {
    		List<IrFastighet> lists = this.entityManager
    				.createQuery(
    						"select irFastigheter from IrFastighet irFastigheter where irFastigheter.fnr = :fnr and irFastigheter.version = :version")
    				.setParameter("version", version).setParameter("fnr", fnr)
    				.getResultList();
    
    		if (lists == null || lists.size() <= 0)
    			return null;
    		// throw new EntityNotFoundException("Ingen IrFastighet med fnr = "
    		// + fnr + " och version = " + version + " finns!");
    		return lists.get(0);
    	}
    
    	@Override
    	public void saveAll(List<IrFastighet> list) throws DataAccessException {
    		for (IrFastighet irFastighet : list) {
    			this.save(irFastighet);
    		}
    	}
    
    	@Override
    	public int getLatestAvailableVersionOfIrFastighet(String fnr) {
    
    		List<Integer> lists = this.entityManager
    				.createQuery(
    						"select max(irf.version) from IrFastighet irf where irf.fnr = :fnr")
    				.setParameter("fnr", fnr).getResultList();
    
    		if (lists == null || lists.size() <= 0)
    			return 0;
    		return lists.get(0) == null ? 0 : lists.get(0);
    	}
    
    }

  3. #13
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You use both @Autowired and @Resource on your entitymanager...

    Code:
    @Autowired
    @Resource(mappedName = "milinsEntityManager")
    protected EntityManager entityManager;
    Don't... Use @PersistenceContext instead or use either one both not both...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #14

    Default

    Quote Originally Posted by Marten Deinum View Post
    You use both @Autowired and @Resource on your entitymanager...

    Code:
    @Autowired
    @Resource(mappedName = "milinsEntityManager")
    protected EntityManager entityManager;
    Don't... Use @PersistenceContext instead or use either one both not both...
    Thanks alot Marten!
    I used

    Code:
    @PersistenceContext(unitName = "milins-domain")
    protected EntityManager entityManager;
    and it works.
    Thanks for all your time you spend to help me :-)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •