我正在尝试将自定义方法添加到我的spring存储库,但它会抛出错误。以下是我正在使用的代码片段。 CommonDAO有我正在尝试添加的额外方法。用户是我尝试操作的实体。问题是Spring将方法视为用户实体的属性而不是单独的方法。能不能让我知道我错过了什么?
存储库接口和实现
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
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;
}
}
测试类
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());
}
}
我收到的错误 找不到类型用户的属性合并!
15:25:03,995 INFO a.LocalContainerEntityManagerFactoryBean:462 - 关闭持久性单元&#39; testPU&#39;的JPA EntityManagerFactory 线程&#34; main&#34;中的例外情况 org.springframework.beans.factory.BeanCreationException:错误 使用名称&#39; userDAOJPA创建bean&#39;:调用init方法 失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException:没有 找到类型用户的属性合并!在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 在 org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139) 在 org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83) 在com.debopam.sprintx.TestTx.main(TestTx.java:14)引起: org.springframework.data.mapping.PropertyReferenceException:没有 找到类型用户的属性合并!在 org.springframework.data.mapping.PropertyPath。(PropertyPath.java:75) 在 org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) 在 org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) 在 org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) 在 org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241) 在 org.springframework.data.repository.query.parser.Part。(Part.java:76) 在 org.springframework.data.repository.query.parser.PartTree $ OrPart。(PartTree.java:235) 在 org.springframework.data.repository.query.parser.PartTree $ Predicate.buildTree(PartTree.java:373) 在 org.springframework.data.repository.query.parser.PartTree $谓词(PartTree.java:353) 在 org.springframework.data.repository.query.parser.PartTree。(PartTree.java:84) 在 org.springframework.data.jpa.repository.query.PartTreeJpaQuery。(PartTreeJpaQuery.java:61) 在 org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95) 在 org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:206) 在 org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:73) 在 org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor。(RepositoryFactorySupport.java:347) 在 org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185) 在 org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239) 在 org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225) 在 org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ......还有12个
答案 0 :(得分:1)
您的班级UserDAOImplJPA
被错误地命名。实际名称应为UserDAOJPAImpl
请从文档中找到以下内容。
找到类的最重要的一点是它的名称的Impl后缀与核心存储库接口相比(见下文)。
有关详细信息,请参阅Spring Docs。