Untitled

 avatar
unknown
java
10 months ago
1.5 kB
6
Indexable
package it.example;

import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

@SuppressWarnings("ALL")
public class ExampleForAlex {


    @Getter
    @Setter
    @Entity
    @Table(name = "customer")
    static class Customer {
        private Long id;
        private String name;
        private String surname;
    }


    @Getter
    @Setter
    @Entity
    @Table(name = "card")
    static class Card {
        private Long id;
        private Long customerForeignKey;
        private String cardNumber;
    }


    @Data
    @RequiredArgsConstructor
    static class CardCustomerDto {
        private final Customer customer;
        private final Card card;
    }


    public static interface CustomerRepository extends JpaRepository<Customer, Long> {

        @Query("""
                    SELECT new it.example.ExampleForAlex$CardCustomerDto(customer, card)
                    FROM Customer customer
                    JOIN Card card ON card.customerForeignKey = customer.id
                    WHERE customer.name = :name
                """)
        List<CardCustomerDto> findByCustomerName(
                @Param("name") String name
        );
    }
}
Editor is loading...
Leave a Comment