java 使用 JpaRepository 接口。如何让实体管理器设置属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28945053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:26:12  来源:igfitidea点击:

Using JpaRepository interface. How can I get the entitymanager to set property?

javajpaspring-data

提问by Felipe Ferreira do Amaral

All DAO's in my system is only interfaces, like this:

我系统中的所有 DAO 都只是接口,如下所示:

public interface UserDAO extends JpaRepository<User, String> {
    User findByUsername(String username);
    Page<User> findAll(Pageable pageable);
}

And that's all.

就这样。

But now I need to set a property at entitymanager. How can I get the entitymanager in my interface?

但是现在我需要在 entitymanager 中设置一个属性。如何在我的界面中获取实体管理器?

The bean is:

豆是:

<bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory" />

But I need to set this property at my DAO because this property depends of the logged user.

但是我需要在我的 DAO 中设置这个属性,因为这个属性取决于登录的用户。

回答by Alan Hay

You will need to create a custom repository as detailed in the spring-data documentation. This can be done for a single repository or for all repositories.

您将需要创建一个自定义存储库,如 spring-data 文档中所述。这可以为单个存储库或所有存储库完成。

The process is clearly outlined below.

下面清楚地概述了该过程。

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour