jpa 跨表_在春季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

public interface BEntityRepository extends JpaRepository

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();

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.

Can anyone please help.

I am using mysql.

解决方案I understand that there is separate repository for each entity(table)

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/

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);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值