在春季jparepository中加入多个表 [英] join more than one table in spring jparepository

查看:257
本文介绍了在春季jparepository中加入多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过加入来获取记录.我是春季jparepository的新手. 我知道每个实体(表)都有一个单独的存储库,在实现时,我需要定义主键的实体和数据类型.

I am trying to fetch record by doing a join. I am new to spring jparepository. I understand that there is separate repository for each entity(table) where when i implement i need to define the entity and datatype of primary key.

任何人都可以建议我如何通过连接两个表来获取记录.

Could anyone please suggest how can I fetch record by joining two tables.

我有两个回购协议,如下所示:

I have two repo as below:

public interface AEntityRepository extends JpaRepository<AEntity, Integer>

public interface BEntityRepository extends JpaRepository<BEntity, Integer>

我想加入以上两个实体(AEntity,BEntity). 我知道我可以使用如下所示的自定义查询:

I want to join above two entity(AEntity, BEntity). I know I can have custom query using something like below:

@Query("SELECT ****** FROM AEntity ae")
AEntity findCustomrRecords();

但是我可以用join编写相同类型的查询(join查询). 我是否需要一个单独的存储库来实现其他一些类.

However can I write the same kind of query (join query) with join. Do i need to have a separate repository implementing some other class.

任何人都可以帮忙.

我正在使用mysql.

I am using mysql.

推荐答案

我了解每个实体(表)都有单独的存储库

I understand that there is separate repository for each entity(table)

这是一个非常普遍的误解.您不希望每个实体都有一个存储库,而是每个聚合根都有一个存储库.参见 http://static.olivergierke.de/lectures/ddd-and-spring/

This is a very common misunderstanding. You do not want to have a repository per entity, but per aggregate root. See http://static.olivergierke.de/lectures/ddd-and-spring/

关于您的特定问题:在存储库界面中创建自定义方法并使用JPQL进行注释应该可以解决问题.这样您会得到类似的信息:

Regarding your specific problem at hand: Creating a custom method in your repository interface and annotating it with a JPQL should do the trick. So you get something like:

@Query("select a from BEntity b join b.a a where b.foo = :foo")
AEntity getAllFooishAs(String foo);

您可以在查询中使用任何 JPQL提供的联接语法

You can use any join syntax JPQL offers in the query.

这篇关于在春季jparepository中加入多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆