【问题标题】:How to use generics with JpaRepository of spring data jpa [duplicate]如何将泛型与spring data jpa的JpaRepository一起使用[重复]
【发布时间】:2018-11-20 11:40:58
【问题描述】:
//generic repo
public interface MyGenericRepo extends JpaRepository<GenericEntity,Integer> { }

//entity
class Place extends GenericEntity {
    private Event event;
}

//entity
class Event extends GenericEntity{  }

//entity
class Offer extends GenericEntity
{
    private Place place;
}

//entity
class User extends GenericEntity {
    private Place place;
}

我应该在 GenericEntity 中使用什么以及如何创建 ModelManager 来保存和加载实体

【问题讨论】:

  • 是的,类似,但是请教如何创建服务

标签: java spring-data-jpa


【解决方案1】:

如果您不想使用Integer 作为密钥创建自己的存储库接口。你必须定义:

@NoRepositoryBean
public interface MyGenericRepo<T> extends JpaRepository<T, Integer> {
}

需要注解@NoRepositoryBean 以避免创建存储库实现。您可以在https://stackoverflow.com/a/11585811/3058413 上阅读更多内容。

之后你应该为每个实体划分接口:

public interface PlaceRepository extends MyGenericRepo<Place> {

}

Spring 数据会自动创建此存储库的实现。

【讨论】:

  • 我不想为每个实体创建存储库
  • 您无法生成实体类,因为 spring 无法终止它必须使用的实体。
  • 如果有请举例
猜你喜欢
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2017-08-26
  • 2013-03-05
  • 2019-02-09
  • 1970-01-01
  • 2020-03-09
相关资源
最近更新 更多