gpt4 book ai didi

java - Spring JPA添加自定义方法

转载 作者:行者123 更新时间:2023-11-30 08:16:40 24 4
gpt4 key购买 nike

我正在尝试将自定义方法添加到我的 spring 存储库,但它抛出错误。以下是我正在使用的代码片段。 CommonDAO 有我正在尝试添加的额外方法。用户是我要操作的实体。问题是Spring将该方法视为用户实体的属性而不是单独的方法。你能让我知道我缺少什么吗?

存储库接口(interface)和实现

    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());
}
}

我收到错误未找到 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

最佳答案

您的类UserDAOImplJPA 命名错误。实际名称应为 UserDAOJPAImpl

请从文档中找到以下内容。

与核心存储库接口(interface)相比,要找到的类最重要的一点是其名称的 Impl 后缀(见下文)。

引用Spring Docs了解更多信息。

关于java - Spring JPA添加自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29558753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com