Untitled

mail@pastecode.io avatar
unknown
java
24 days ago
1.5 kB
4
Indexable
Never
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
        );
    }
}
Leave a Comment