开发者

@Transactional annotation causing incompatible return types error on entity

I have a Restful web service developed in Spring MVC which currently returns farmer information and allows for the deletion and addition of new farmers to the database. When extending the web service to include farmer advisors I am recieving the following error as soon as I add the transactional annotation to the advisor DAO implementation:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'advisorDAO' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: methods with same signature getAdvisors() but incompatible return types: [interface java.util.List, class [Lorg.springframework.aop.Advisor;]

The odd part about this error is that the system compiles 开发者_高级运维fine and as intended prior to the annotation being added to the class, however as I need to be able to persist the entity to the database the transactions are a requirement. I know what the error means but I am at a loss as to why this is only an issue when using the annotation which isn't even applied to the method the compiler is complaining about.

The Advisors DAO interface:

public interface AdvisorDAO {
   public List<Advisor> getAdvisors();
   public Advisor getAdvisorByPk(int id);   
   public Advisor getAdvisorByFarmerID(int id);
   public Advisor getAdvisorByAdvisorID(int id);    
   public void saveAdvisor(Advisor advisor);
   public void deleteAdvisor(Advisor advisor);
   public void updateAdvisor (Advisor advisor);
}

The interface implementation:

public class JpaAdvisorDAO implements AdvisorDAO {

@PersistenceContext
private EntityManager entityManager;

public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Override
public List<Advisor> getAdvisors() {
    return entityManager.createNamedQuery("Advisor.findAll").getResultList();
}

@Override
public Advisor getAdvisorByPk(int id) {
    Query query = entityManager.createNamedQuery("Advisor.findByPK");
    query.setParameter("advisorPk", id);
    return (Advisor) query.getSingleResult();
}   

@Override
public Advisor getAdvisorByFarmerID(int id) {
    Query query = entityManager.createNamedQuery("Advisor.findByFarmerId");
    query.setParameter("farmerId", id);
    return (Advisor) query.getSingleResult();
}

@Override
public Advisor getAdvisorByAdvisorID(int id) {
    Query query = entityManager.createNamedQuery("Advisor.findByAdvisorId");
    query.setParameter("advisorId", id);
    return (Advisor) query.getSingleResult();  
}    

@Override
@Transactional
public void saveAdvisor(Advisor advisor) {
    entityManager.persist(advisor);
}

@Override
@Transactional
public void deleteAdvisor(Advisor advisor) {
    entityManager.remove(entityManager.getReference(Advisor.class, advisor.getAdvisorPk()));

}

@Override
@Transactional
public void updateAdvisor (Advisor advisor) {
    entityManager.merge(advisor);
}

The context file used:

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc">


<bean id="farmerDAO" class="com.test.cmsservice.persistance.JpaFarmerDAO" />
<bean id="advisorDAO" class="com.test.cmsservice.persistance.JpaAdvisorDAO" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="SpringRestService"/>
    <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>
                <property name="generateDdl" value="false"/>
                <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
            </bean>
        </property>
</bean> 
<context:annotation-config />
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
    <property name="url" value="jdbc:derby://localhost:1527/SpringDBTest"/>
    <property name="username" value="APP"/>
    <property name="password" value="app"/>
  </bean>
  <tx:annotation-driven />
  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="dataSource"/>
  </bean>
  <bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />


Adding the annotation can't change the return type.

First, you have probably imported the wrong type - Advisor is a spring class as well, so fix your imports.

Then I suspect that you have an older version of your interface or its implementation that is loaded by the classloader. The exception message says that one of the methods returns a List<Advisor> and the other - Advisor[]. Make sure everything is cleaned and up-to-date.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜