简体   繁体   中英

How do I Write joins in spring boot

I am a beginner in spring boot and I am working on database entities. Now I am in a situation where I need to use join s in my spring boot project to fetch data from different tables.

I have 3 tables item , category and issued_item and I write a query for it to fetch data from it.

The query is:

SELECT issued_item.issued_id,
       item.item_name,
       issued_item.issued_from,
       issued_item.issued_to,
       issued_item.quantity,
       category.cat_type,
       issued_item.issued_date
FROM issued_item
INNER JOIN item ON issued_item.ii_fk = item.item_id
INNER JOIN category ON item.ic_fk = category.cat_id;

Now I am confused about how can I write it on my spring boot project.

Moreover, bring it to some advanced level I use the parameters to access items like:

SELECT issued_item.issued_id,
       item.item_name,
       issued_item.issued_from,
       issued_item.issued_to,
       issued_item.quantity,
       category.cat_type,
       issued_item.issued_date
FROM issued_item
INNER JOIN item ON issued_item.ii_fk = item.item_id
INNER JOIN category ON item.ic_fk = category.cat_id
WHERE issued_item.issued_date BETWEEN FromDate AND ToDate
ORDER BY issued_item.issued_date ASC;

FromDate and ToDate are two different parameters.


Note: If someone needs the entities code then I'll provide it.

Is the use of dto classes useful in this case?

Write as below use return type as List<Object []> .

@Query(value="SELECT issued_item.issued_id, 
item.item_name, issued_item.issued_from, 
issued_item.issued_to, issued_item.quantity, category.cat_type, 
issued_item.issued_date
FROM issued_item
INNER JOIN item
ON issued_item.ii_fk = item.item_id
INNER JOIN category
ON item.ic_fk = category.cat_id
WHERE issued_item.issued_date BETWEEN :fromdate AND 
:todate
ORDER BY issued_item.issued_date ASC", nativeQuery= true)
List<Object []> returnObject(@Param("fromdate" Date fromdate, @Param("todate") Date todate );

Then do the iterations get the values.

for(Object[] obj : result) {
 String issueId = (String) obj[0];
 //Get others variable also
}

The entity classes and repository classes should be similar to as shown below. The OneToMany and the ManyToOne provides the primary key/foreign keys information to JPA to join tables. Since you have table field names that do not follow the typical naming convention, it is essential to explicitly name them via JoinColumn.

Once you have the entities defined, most queries are automatically generated by JPA - you need to only provide the method declaration in the format that JPA requires

@Entity
@Table(name="item")
class Item{
  @Identity
  private Long item_id;

  ....other columns

  @OneToMany(fetch = FetchType.EAGER)
  private List<IssuedItem> issuedItems;

  @ManyToOne
  @JoinColumn(name="ic_fk")
  private Category category;
}

@Entity
@Table(name="category")
class Category{
  @Identity
  private Long cat_id;

  @OneToMany(fetch = FetchType.EAGER)
  private List<Item> items;

  ....other columns

  
}

@Entity
@Table(name="issued_item")
class IssuedItem{

  ....other columns

  
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name="ii_fk")
  private Item item;
}


@Repository
class IssuedItemRepository extends JpaRepository<IssuedItem, Long>{
  //implementation will be generated by JPA
  List<IssuedItem> findAllByIssuedDateBetween(
      Date FromDate,
      Date ToDate);
}

@Repository
class ItemRepository extends JpaRepository<Item, Long>{
}

@Repository
class CategoryRepository extends JpaRepository<Category, Long>{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM