Spring Data Jpa Property Expressions One To Many

drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
drop table IF EXISTS apple;
create TABLE apple (  apple_id int NOT NULL,  apple_name varchar(255) DEFAULT NULL,  PRIMARY KEY (apple_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

drop table IF EXISTS vendor;
create TABLE vendor (  vendor_id int NOT NULL,  vendor_name varchar(255) DEFAULT NULL,  PRIMARY KEY (vendor_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

drop table IF EXISTS apple_vendor;
create TABLE apple_vendor (  apple_apple_id int NOT NULL,  vendor_vendor_id int NOT NULL,  UNIQUE KEY UK_lyk5ineqxr7amhtgiykaoksal (vendor_vendor_id),  KEY FKjv834q86uhttva8j02mtj1fx4 (apple_apple_id),  CONSTRAINT FKbatnqh27ulggq1ps994be78y9 FOREIGN KEY (vendor_vendor_id) REFERENCES vendor (vendor_id),  CONSTRAINT FKjv834q86uhttva8j02mtj1fx4 FOREIGN KEY (apple_apple_id) REFERENCES apple (apple_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

drop table IF EXISTS hibernate_sequence;
create TABLE hibernate_sequence (  next_val bigint DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
insert into apple values (3,'Macintosh'),(6,'Fuji');
insert into vendor values (1,'Shri Seller'),(2,'Vishnu Seller'),(4,'Om Seller'),(5,'Shri Seller');
insert into apple_vendor values (3,1),(3,2),(6,4),(6,5);
insert into hibernate_sequence values (7),(7);
package com.sujan.example.jpa.controller;

import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
import com.sujan.example.jpa.repository.VendorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HelloController {
    @Autowired
    private AppleRepository appleRepository;
    @Autowired
    private VendorRepository vendorRepository;

    @GetMapping
    public List<Apple> findAllByVendor_VendorName() {
        List<Apple> apple = appleRepository.findAllByVendor_VendorName("Vishnu Seller");
        System.out.println("findAllByVendor_VendorName = " + apple);
        return apple;
    }
}
package com.sujan.example.jpa.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.List;

@Data
@Entity
@NoArgsConstructor
@Table(name = "Apple")
public class Apple {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int appleId;
    String appleName;

    @OneToMany(cascade = CascadeType.ALL)
    List<Vendor> vendor;

    public Apple(String appleName) {
        this.appleName = appleName;
    }
}
package com.sujan.example.jpa.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Entity
@NoArgsConstructor
@Table(name = "Vendor")
public class Vendor {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int vendorId;
    String vendorName;

    public Vendor(String vendorName) {
        this.vendorName = vendorName;
    }
}
package com.sujan.example.jpa.repository;

import com.sujan.example.jpa.entity.Apple;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AppleRepository extends JpaRepository<Apple, Long> {
    List<Apple> findAllByVendor_VendorName(String vendorName);
}
package com.sujan.example.jpa.repository;

import com.sujan.example.jpa.entity.Vendor;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface VendorRepository extends JpaRepository<Vendor, Long> {
}
package com.sujan.example.jpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
}
spring.datasource.url=jdbc:mysql://localhost:3306/AppleDb
spring.datasource.username=root
spring.datasource.password=root
plugins {
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}
group = 'com.sujan'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
test {
    useJUnitPlatform()
}
curl --location --request GET 'http://localhost:8080/'
select apple0_.apple_id as apple_id1_0_, apple0_.apple_name as apple_na2_0_ from apple apple0_ left outer join apple_vendor vendor1_ on apple0_.apple_id=vendor1_.apple_apple_id left outer join vendor vendor2_ on vendor1_.vendor_vendor_id=vendor2_.vendor_id where vendor2_.vendor_name=?
Hibernate: select vendor0_.apple_apple_id as apple_ap1_1_0_, vendor0_.vendor_vendor_id as vendor_v2_1_0_, vendor1_.vendor_id as vendor_i1_2_1_, vendor1_.vendor_name as vendor_n2_2_1_ from apple_vendor vendor0_ inner join vendor vendor1_ on vendor0_.vendor_vendor_id=vendor1_.vendor_id where vendor0_.apple_apple_id=?
findAllByVendor_VendorName = [Apple(appleId=3, appleName=Macintosh, vendor=[Vendor(vendorId=1, vendorName=Shri Seller), Vendor(vendorId=2, vendorName=Vishnu Seller)])]

follow us on