Spring Data Jpa Property Expressions One To One

package com.sujan.example.jpa.controller;

import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
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;

    @GetMapping
    public List<Apple> findAllByVendorVendorName() {
        List<Apple> findAllByVendorVendorName = appleRepository.findAllByVendorVendorName("Shri Vendor");
        System.out.println("findAllByVendorVendorName = " + findAllByVendorVendorName);
        return findAllByVendorVendorName;
    }
}
package com.sujan.example.jpa.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Data
@Entity
@NoArgsConstructor
@Table(name = "Apple")
public class Apple {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int appleId;
    String appleName;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vendorId")
    private Vendor vendor;
}
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;
}
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> findAllByVendorVendorName(String vendorName);
}
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_, apple0_.vendor_id as vendor_i3_0_ from apple apple0_ left outer join vendor vendor1_ on apple0_.vendor_id=vendor1_.vendor_id where vendor1_.vendor_name=?
Hibernate: select vendor0_.vendor_id as vendor_i1_1_0_, vendor0_.vendor_name as vendor_n2_1_0_ from vendor vendor0_ where vendor0_.vendor_id=?
findAllByVendorVendorName = [Apple(appleId=2, appleName=Macintosh, vendor=Vendor(vendorId=1, vendorName=Shri Vendor))]

findAllByVendorVendorName method will work, but if there is variable name vendorVendor in Apple class, then spring will confuse how to create query. Apple class vendorVendor variable or Vendor class VendorName variable.

this will throw error Failed to create query for method findAllByVendorVendorName(String)! No property name found for type String! Traversed path: Apple.vendorVendor.

drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
drop table if exists apple;
drop table if exists hibernate_sequence;
drop table if exists vendor;
create table apple (apple_id integer not null, apple_name varchar(255), vendor_vendor varchar(255), vendor_id integer, primary key (apple_id)) engine=InnoDB;
create table hibernate_sequence (next_val bigint) engine=InnoDB;
create table vendor (vendor_id integer not null, vendor_name varchar(255), primary key (vendor_id)) engine=InnoDB;
alter table apple add constraint FKb04bbvng0f19nvgu0mq9bq3y7 foreign key (vendor_id) references vendor (vendor_id);
insert into vendor (vendor_id, vendor_name ) values (1,'Shri Vendor');
insert into apple (apple_id, apple_name, vendor_vendor, vendor_id) values (2,'Macintosh','Macintosh Vendor', 1);
insert into vendor (vendor_id,vendor_name ) values (3,'Vishnu Vendor');
insert into apple (apple_id, apple_name, vendor_vendor, vendor_id) values (4,'Fuji','Fuji Vendor',3);
insert into hibernate_sequence values ( 5 );
package com.sujan.example.jpa.controller;

import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
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;

    @GetMapping
    public void findAllByVendor() {
        List<Apple> findAllByVendorVendor = appleRepository.findAllByVendorVendor("Macintosh Vendor");
        System.out.println("findAllByVendorVendor = " + findAllByVendorVendor);
        List<Apple> findAllByVendorVendorName = appleRepository.findAllByVendorVendorName("Shri Vendor");
        System.out.println("findAllByVendorVendorName = " + findAllByVendorVendorName);
    }
}
package com.sujan.example.jpa.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Data
@Entity
@NoArgsConstructor
@Table(name = "Apple")
public class Apple {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int appleId;
    String appleName;
    String vendorVendor;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vendorId")
    private Vendor vendor;
}
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;
}
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> findAllByVendorVendor(String vendorName);
    List<Apple> findAllByVendorVendorName(String vendorName);
}
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/'

best way to deal with collection class is to use _ to distinguish between them.

drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
drop table if exists apple;
drop table if exists hibernate_sequence;
drop table if exists vendor;
create table apple (apple_id integer not null, apple_name varchar(255), vendor_vendor varchar(255), vendor_id integer, primary key (apple_id)) engine=InnoDB;
create table hibernate_sequence (next_val bigint) engine=InnoDB;
create table vendor (vendor_id integer not null, vendor_name varchar(255), primary key (vendor_id)) engine=InnoDB;
alter table apple add constraint FKb04bbvng0f19nvgu0mq9bq3y7 foreign key (vendor_id) references vendor (vendor_id);
insert into vendor (vendor_id, vendor_name ) values (1,'Shri Vendor');
insert into apple (apple_id, apple_name, vendor_vendor, vendor_id) values (2,'Macintosh','Macintosh Vendor', 1);
insert into vendor (vendor_id,vendor_name ) values (3,'Vishnu Vendor');
insert into apple (apple_id, apple_name, vendor_vendor, vendor_id) values (4,'Fuji','Fuji Vendor',3);
insert into hibernate_sequence values ( 5 );
package com.sujan.example.jpa.controller;

import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
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;

    @GetMapping
    public void findAllByVendor() {
        List<Apple> findAllByVendorVendor = appleRepository.findAllByVendorVendor("Macintosh Vendor");
        System.out.println("findAllByVendorVendor = " + findAllByVendorVendor);
        List<Apple> findAllByVendor_VendorName = appleRepository.findAllByVendor_VendorName("Shri Vendor");
        System.out.println("findAllByVendor_VendorName = " + findAllByVendor_VendorName);
    }
}
package com.sujan.example.jpa.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Data
@Entity
@NoArgsConstructor
@Table(name = "Apple")
public class Apple {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int appleId;
    String appleName;
    String vendorVendor;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vendorId")
    private Vendor vendor;
}
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;
}
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> findAllByVendorVendor(String vendorName);
    List<Apple> findAllByVendor_VendorName(String vendorName);
}
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_, apple0_.vendor_id as vendor_i4_0_, apple0_.vendor_vendor as vendor_v3_0_ from apple apple0_ where apple0_.vendor_vendor=?
select vendor0_.vendor_id as vendor_i1_1_0_, vendor0_.vendor_name as vendor_n2_1_0_ from vendor vendor0_ where vendor0_.vendor_id=?
findAllByVendorVendor = [Apple(appleId=2, appleName=Macintosh, vendorVendor=Macintosh Vendor, vendor=Vendor(vendorId=1, vendorName=Shri Vendor))]
select apple0_.apple_id as apple_id1_0_, apple0_.apple_name as apple_na2_0_, apple0_.vendor_id as vendor_i4_0_, apple0_.vendor_vendor as vendor_v3_0_ from apple apple0_ left outer join vendor vendor1_ on apple0_.vendor_id=vendor1_.vendor_id where vendor1_.vendor_name=?
findAllByVendor_VendorName = [Apple(appleId=2, appleName=Macintosh, vendorVendor=Macintosh Vendor, vendor=Vendor(vendorId=1, vendorName=Shri Vendor))]

follow us on