BAEL-7692 added case for ResultDTO_wo_Ids.java

This commit is contained in:
Zeeshan Arif 2024-03-31 18:04:22 +05:00
parent 35bdac1c59
commit fbef22d79d
3 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,66 @@
package com.baeldung.spring.data.jpa.joinquery.DTO;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import java.time.LocalDate;
//@Entity
//@IdClass(DTO.class)
public class ResultDTO_wo_Ids {
public ResultDTO_wo_Ids(String customerName, String customerEmail, LocalDate orderDate, String productName, Double productPrice) {
this.customerName = customerName;
this.customerEmail = customerEmail;
this.orderDate = orderDate;
this.productName = productName;
this.productPrice = productPrice;
}
private String customerName;
private String customerEmail;
private LocalDate orderDate;
private String productName;
private Double productPrice;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerEmail() {
return customerEmail;
}
public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
public LocalDate getOrderDate() {
return orderDate;
}
public void setOrderDate(LocalDate orderDate) {
this.orderDate = orderDate;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Double getProductPrice() {
return productPrice;
}
public void setProductPrice(Double productPrice) {
this.productPrice = productPrice;
}
}

View File

@ -1,6 +1,7 @@
package com.baeldung.spring.data.jpa.joinquery.repositories;
import com.baeldung.spring.data.jpa.joinquery.DTO.ResultDTO;
import com.baeldung.spring.data.jpa.joinquery.DTO.ResultDTO_wo_Ids;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@ -16,6 +17,13 @@ public interface JoinRepository extends CrudRepository<ResultDTO, Long> {
+ " and c.id=?1 ")
List<ResultDTO> findResultDTOByCustomer(Long id);
@Query(value = "SELECT new com.baeldung.spring.data.jpa.joinquery.DTO.ResultDTO_wo_Ids(c.name, c.email, o.orderDate, p.productName, p.price) "
+ " from Customer c, CustomerOrder o ,Product p "
+ " where c.id=o.customer.id "
+ " and o.id=p.customerOrder.id "
+ " and c.id=?1 ")
List<ResultDTO_wo_Ids> findResultDTOByCustomerWithoutIds(Long id);
@Query(value = "SELECT c.*, o.*, p.* "
+ " from Customer c, CustomerOrder o ,Product p "
+ " where c.id=o.customer_id "

View File

@ -1,5 +1,6 @@
package com.baeldung.spring.data.jpa.joinquery;
import com.baeldung.spring.data.jpa.joinquery.DTO.ResultDTO_wo_Ids;
import com.baeldung.spring.data.jpa.joinquery.entities.Customer;
import com.baeldung.spring.data.jpa.joinquery.entities.CustomerOrder;
import com.baeldung.spring.data.jpa.joinquery.DTO.ResultDTO;
@ -90,6 +91,13 @@ class JoinRepositoryTest {
assertEquals(9, map.keySet().size());
});
}
@Test
void whenFindResultDTOByCustomerWithoutIds_thenReturnResultDTO() {
List<ResultDTO_wo_Ids> resultDTO = joinRepository.findResultDTOByCustomerWithoutIds(1L);
assertInstanceOf(ResultDTO_wo_Ids.class, resultDTO.get(0));
}
private EntityTransaction getTransaction() {
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();