【问题标题】:Custom method in hibernate repository休眠存储库中的自定义方法
【发布时间】:2014-04-14 21:18:23
【问题描述】:

我想添加自定义方法来休眠CrudRepository。我将它与弹簧框架一起使用。 Documentation 表示可以,但是示例显示接口未扩展Repository 基类。

我知道有一个注释@Query 可以定义自定义查询,但是如果我想将例如Criteria 查询添加到该类怎么办?我也希望像findAll 这样的方法留在存储库中。

这是我尝试的。首先,我为Post 实体创建了存储库:

package foo.bar.repository;

import foo.bar.model.Post;
import org.springframework.data.repository.CrudRepository;

public interface PostRepository extends CrudRepository<Post, Long> {

}

然后我创建了扩展接口的抽象实现类并将我的自定义方法添加到它:

package foo.bar.repository;

public abstract class PostRepositoryImpl implements PostRepository {

    public List<Post> someCustomMethod() {
        //some logic here
    }

}

甚至可以这样做吗?也许在休眠中有其他方法可以做到这一点。我长期使用Doctrine,它基于hibernate。在那里,您只需扩展基础存储库类并添加您想要的方法。

【问题讨论】:

    标签: spring hibernate repository


    【解决方案1】:

    我将添加一个执行自定义查询的自定义方法示例。我希望我能理解您的问题的意思。

    PersonRepository

    public interface PostRepository extends CrudRepository<Post, Long>{
        List<Post> getPostsOlderThanDate(Date date);
    }
    

    PersonRepositoryImpl

    import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
    
    import javax.persistence.EntityManager;
    import javax.persistence.TypedQuery;
    import java.util.Date;
    import java.util.List;
    
    public class PostRepositoryImpl extends SimpleJpaRepository<Post, Long> implements PostRepository {
    
        private EntityManager entityManager;
    
        public PostRepositoryImpl(EntityManager em) {
            super(Post.class, em);
            this.entityManager = em;
        }
    
        @Override
        public List<Post> getPostsOlderThanDate(Date date) {
            String query = ""; //create query
            TypedQuery<Post> typedQuery = entityManager.createQuery(query, Post.class);
            return typedQuery.getResultList();
        }
    }
    

    然后您创建 EntityManagerFactory bean(您使用 Hibernate 持久性提供程序构建它,或者如何设置它,这取决于您),并将其注入到 PersonRepositoryImpl bean 的构造函数中。

    <bean id="personRepository" class="com.package.PersonRepositoryImpl">
        <constructor-arg ref="entityManagerFactory" />
    </bean>
    

    这些是向您的数据库提供一些自定义查询的步骤。

    注意:如果您在设置 EntityManager 时需要帮助,请给我留言,我会为您提供 spring 配置和依赖项。

    注意,后缀“Impl”很重要。或者它可以被调整。在这篇文章中找到“repository-impl-postfix”:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.create-instances.spring 了解更多详情。

    repository-impl-postfix
    
    Defines the postfix to autodetect custom repository implementations. Classes whose names end with the configured postfix are considered as candidates. Defaults to Impl.
    

    【讨论】:

    • 您好 @gabriel-ruiu ,我找不到按照您的建议添加 PersonRepositoryImpl bean 的位置,这还需要做吗?
    • @AdhamSabry,bean 定义在 applicationContext.xml 文件中。这是在 Spring 中定义 bean 的方法之一。也许这会有所帮助 ->stackoverflow.com/questions/28414234/… 。但在谷歌上搜索“Spring xml 配置”也可能会产生一些信息。
    • 关于如何添加“查找策略”方法的任何想法?可选 findPostByPostNameEquals(String postname);如果我将此代码放在“接口 PostRepository”中,则 PostRepositoryImpl 需要一个实现。但这违背了“查找策略”的原则:(docs.spring.io/spring-data/jpa/docs/current/reference/html/…
    猜你喜欢
    • 2018-02-09
    • 2018-01-05
    • 2020-04-20
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2015-08-17
    相关资源
    最近更新 更多