Announcement Announcement Module
Collapse
No announcement yet.
Spring, Hibernate-based JPA: error creating dao bean ... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring, Hibernate-based JPA: error creating dao bean ...

    This program used to run well, but it didn't work this morning ?!?

    This is my persistence.xml (copied from Seam):
    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="demoDatabase" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <properties>
                <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
                <property name="hibernate.connection.username" value="hannah"/>
                <property name="hibernate.connection.password" value="hannah"/>
                <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
                <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
                <property name="hibernate.hbm2ddl.auto" value="update"/>
                <property name="hibernate.show_sql" value="true"/>
                <property name="hibernate.format_sql" value="true" />
            </properties>
        </persistence-unit>
    </persistence>
    Note: I used a different way to define a persistence unit (maybe copied from Spring) and it also worked well. A small question here: is this an efficient definition or is there something better?

    These are some JPA daos:
    Code:
    public class GeoAreaDaoJpa extends GenericDaoJpa<GeoArea, Long> implements GeoAreaDao {
        public GeoAreaDaoJpa() {
            super(GeoArea.class);
        }
    }
    
    public class GenericDaoJpa<T, PK extends Serializable> implements GenericDao<T, PK> {
        protected final Log log = LogFactory.getLog(getClass());
    
        @PersistenceContext
        protected EntityManager entityManager;
        private Class<T> persistentClass;
    
        public GenericDaoJpa(Class<T> persistentClass) {
            this.persistentClass = persistentClass;
        }
    
        @SuppressWarnings("unchecked")
        public List<T> getSome(int pageNo, int noRowsPerPage) {
            Query q = entityManager.createQuery("select obj from " + persistentClass.getName() + " obj");
            if (pageNo > 0 && noRowsPerPage > 0) {
                q.setFirstResult(noRowsPerPage * (pageNo - 1) + 1)
                    .setMaxResults(noRowsPerPage);
            }
            return q.getResultList();
        }
    
        public T get(PK id) throws EntityNotFoundException {
            T entity = (T) entityManager.find(persistentClass, id);
            if (entity == null) {
                throw new EntityNotFoundException(persistentClass + " object with id " + id + " not found.");
            }
            return entity;
        }
    
        public T save(T object) {
            return entityManager.merge(object);
        }
    
        public void remove(PK id) {
            entityManager.remove(get(id));
        }
    
        public int getNumberOfObjects() {
            Long numberOfObjects = (Long) entityManager.createQuery("select count(obj) from " + persistentClass.getName() + " obj")
                    .getSingleResult();
            return numberOfObjects.intValue();
        }
    
        public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
        }
    }
    Note: the GenericDaoJpa was copied from AppFuse.

    This is the service:
    Code:
    public interface ZipCodeService {
        public List<GeoArea> getGeoAreas(String stateCode);
    }
    
    public class ZipCodeServiceImpl implements ZipCodeService {
        protected final Log log = LogFactory.getLog(getClass());
    
        private GeoAreaDao geoAreaDao;
    
        /**
         * Get the info from the Internet and save to the database
         * @param stateCode
         * @return
         */
        public List<GeoArea> getGeoAreas(String stateCode) {
            return null;
        }
    
        public void setGeoAreaDao(GeoAreaDao geoAreaDao) {
            this.geoAreaDao = geoAreaDao;
        }
    }
    Note: the getGeoAreas is almost empty (infact, it has something inside, but it's too long to post here).

    Next is the junit test:
    Code:
    public class ZipCodeServiceTest extends AbstractTransactionalDataSourceSpringContextTests {
        private ZipCodeService zipCodeService;
    
        public void testZipCodeService() {
            List<GeoArea> geoAreas = zipCodeService.getGeoAreas("TX");
    //        this.setComplete();
        }
    
        public void setZipCodeService(ZipCodeService zipCodeService) {
            this.zipCodeService = zipCodeService;
        }
    
        protected String[] getConfigLocations() {
            return new String[] { "classpath:applicationContext.xml" };
        }               
    }
    And the applicationContext.xml:
    Code:
        <!-- just to satisfy the AbstractTransactionalDataSourceSpringContextTests -->
        <bean id="dataSource"
              class="org.apache.commons.dbcp.datasources.SharedPoolDataSource">
            <property name="maxActive" value="10"/>
            <property name="maxIdle" value="10"/>
            <property name="maxWait" value="-1"/>
            <property name="connectionPoolDataSource">
                <bean class="oracle.jdbc.pool.OracleConnectionPoolDataSource">
                    <property name="URL" value="jdbc:oracle:thin:@localhost:1521:XE" />
                    <property name="user" value="hannah"/>
                    <property name="password" value="hannah"/>
                    <property name="dataSourceName" value=""/>
                </bean>
            </property>
        </bean>
    
        <bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="demoDatabase"/>
        </bean>
    
        <bean id="transactionManager"
              class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
        <bean id="jpaInterceptor" class="org.springframework.orm.jpa.JpaInterceptor">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="save" propagation="REQUIRED"/>
                <tx:method name="remove" propagation="REQUIRED"/>
                <tx:method name="*" propagation="REQUIRED" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    
        <bean id="zipCodeServiceAround" class="demo.advice.ZipCodeServiceAround" />
    
        <aop:config>
            <aop:pointcut id="serviceMethods"
                          expression="execution(* *.service.impl.*.*(..))"/>
            <!-- aop:advisor advice-ref="jpaInterceptor"
                         pointcut="execution(* com.momentumsystems.acegiex.RealTestImpl.*(..))"
                         order="1" / -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" order="1"/>
        </aop:config>
    
        <bean id="geoAreaDao" class="demo.dao.jpa.GeoAreaDaoJpa"/>
    
        <bean id="zipCodeService" class="demo.service.impl.ZipCodeServiceImpl">
            <property name="geoAreaDao" ref="geoAreaDao"/>
        </bean>
    When I run the junit test, this error occured:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'geoAreaDao' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.properties()[Ljavax/persistence/PersistenceProperty;
    Caused by: java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.properties()[Ljavax/persistence/PersistenceProperty;
    	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.addIfPresent(PersistenceAnnotationBeanPostProcessor.java:327)
    	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.access$000(PersistenceAnnotationBeanPostProcessor.java:148)
    	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$1.doWith(PersistenceAnnotationBeanPostProcessor.java:299)
    	at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:283)
    	at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:260)
    	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findAnnotationMetadata(PersistenceAnnotationBeanPostProcessor.java:297)
    	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessAfterInstantiation(PersistenceAnnotationBeanPostProcessor.java:270)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:412)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
    I cannot find where the error is

  • #2
    I would guess you don't have a compatible version of the JAR on the classpath.
    Last edited by karldmoore; Aug 27th, 2007, 03:00 PM.

    Comment

    Working...
    X