1
votes

I am trying to add custom method to my spring repository but it is throwing errors. Below are the code snippets I am using. CommonDAO has the extra method I am trying add. User is the entity on which I am trying to operate. Problem is Spring is treating the method as User entity's property not as a separate method. Could you please let me know what I am missing?

Repository Interfaces and Implementations

    public interface CommonDAO<T> {
              public T merge(T t);
         }

    public interface UserDAOJPA extends JpaRepository<User, Integer>, CommonDAO<User> {
        public User findByUsername(String userName);
    }


@Repository(value="jpaBean")
public class UserDAOImplJPA implements CommonDAO<User> {

    @PersistenceContext
    private EntityManager em;

    @Override
    public User merge(User user) {
        return em.merge(user);
    }
}

Service Class

@Service
public class UserManagerJPAImpl implements UserManager {

    @Autowired
    private UserDAOJPA userDAO;


    @Override
    @Transactional
    public void insertUser(User user) {
        userDAO.save(user);
    }

    @Override
    @Transactional
    public User getUserById(int userId) {
        return userDAO.findOne(userId);
    }

    @Override
    @Transactional
    public User getUser(String username) {
        return userDAO.findByUsername(username);
    }

    @Override
    @Transactional
    public List<User> getUsers() {
        return userDAO.findAll();
    }

    @Override
    @Transactional
    public User mergeUser(User user) {
        return userDAO.merge(user);
    }

    /**
     * @param userDAO the userDAO to set
     */
    public void setUserDAO(UserDAOJPA userDAO) {
        this.userDAO = userDAO;
    }

}

Test Class

public class TestTx {
    public static void main(String[] args) {
        ApplicationContext ctx =
                new ClassPathXmlApplicationContext("app-context.xml");
/*      UserManager userManagerTraditional =
                (UserManager) ctx.getBean("userManagerImpl");*/

        UserManager userManager =
                (UserManager) ctx.getBean("userManagerJPAImpl");

        User user = new User();
        user.setUsername("johndoe");
        user.setName("John Doe");

        userManager.insertUser(user);

        System.out.println("User inserted!");

        user = userManager.getUser("johndoe");

        System.out.println("\nUser fetched by username!"
                + "\nId: " + user.getId()
                + "\nUsername: " + user.getUsername()
                + "\nName: " + user.getName());

        user.setName("After change");
        userManager.mergeUser(user);
        //user = userManagerTraditional.getUserById(user.getId());

        System.out.println("\nUser fetched by ID!"
                + "\nId: " + user.getId()
                + "\nUsername: " + user.getUsername()
                + "\nName: " + user.getName());

        List<User> users = userManager.getUsers();

        System.out.println("\nUser list fetched!"
                + "\nUser count: " + users.size());
    }
}

Errors I am getting No property merge found for type User!

15:25:03,995 INFO a.LocalContainerEntityManagerFactoryBean: 462 - Closing JPA EntityManagerFactory for persistence unit 'testPU' Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAOJPA': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property merge found for type User! at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at com.debopam.sprintx.TestTx.main(TestTx.java:14) Caused by: org.springframework.data.mapping.PropertyReferenceException: No property merge found for type User! at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241) at org.springframework.data.repository.query.parser.Part.(Part.java:76) at org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:235) at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) at org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:353) at org.springframework.data.repository.query.parser.PartTree.(PartTree.java:84) at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:61) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:206) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:73) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:347) at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225) at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ... 12 more

1

1 Answers

1
votes

You class UserDAOImplJPA is named wrongly. The actual name should be UserDAOJPAImpl

Please find below from the documentation.

The most important bit for the class to be found is the Impl postfix of the name on it compared to the core repository interface (see below).

Refer Spring Docs for more information.