【问题标题】:Can I choose the name of my jpa repository?我可以选择我的 jpa 存储库的名称吗?
【发布时间】:2012-10-19 06:07:47
【问题描述】:

我必须为扩展 CrudRepository 的接口命名 有一个特殊的名字(我使用 spring 和 Jpa) 即

public interface UserRepository extends CrudRepository<User, Long> {}

所以在这种情况下,我有一个名为 UserRepository 的实体

我试过注释

@Entity(name="UserCustomRepository")
public interface UserRepository extends CrudRepository<User, Long> {}

但它不起作用......

【问题讨论】:

    标签: spring repository spring-data spring-data-jpa


    【解决方案1】:

    根据文档显然不可能,请参阅1.3.3.1. XML Configuration

    这些 bean 中的每一个都将在派生自接口名称的 bean 名称下注册,因此UserRepository 的接口将在userRepository 下注册。

    为什么需要自定义名称?除了您使用 @Entity(name="UserCustomRepository") 的示例已完全损坏之外,@Entity 用于标记 JPA 实体,而不是 Spring beans/repositories。

    【讨论】:

    • 因为我有 2 个数据源,并且我在两个不同数据库中的某些表具有相同的名称。所以我想使用名称的存储库名称,即 User -> UserRepository ,但如果不可能,我会做 User -> UserRemoteRepository.TNX ...
    • @Shinigami:看起来你的用例是合法的。如果确实不可能,请考虑filling feature request
    猜你喜欢
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多