join more than one table in spring jparepository

谁说我不能喝 提交于 2019-12-05 02:51:06

问题


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>

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.


回答1:


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.



来源:https://stackoverflow.com/questions/43273118/join-more-than-one-table-in-spring-jparepository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!