Set interface is defined in java Collection framework. Set doesn't contains duplicate elements. Set elements cannot be accessed using integer index.
drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
create TABLE apple ( apple_id BIGINT NOT NULL, apple_name VARCHAR(255) DEFAULT NULL,rating int(1) DEFAULT NULL, PRIMARY KEY (apple_id)) ENGINE=INNODB;
create TABLE hibernate_sequence ( next_val BIGINT) ENGINE=INNODB;
insert into hibernate_sequence values ( 1 );
insert into apple (apple_id, apple_name, taste) values (1, "Macintosh", "tangy");
insert into apple (apple_id, apple_name, taste) values (2, "Macintosh", "tangy");
insert into apple (apple_id, apple_name, taste) values (3, "Macintosh", "sweet");
insert into apple (apple_id, apple_name, taste) values (4, "Macintosh", "sweet");
insert into apple (apple_id, apple_name, taste) values (5, "Macintosh", "juicy");
insert into apple (apple_id, apple_name, taste) values (6, "Macintosh", "juicy");
insert into apple (apple_id, apple_name, taste) values (7, "Macintosh", "tart");
insert into apple (apple_id, apple_name, taste) values (8, "Macintosh", "tart");
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.Set;
@RestController
public class AppleController {
@Autowired
private AppleRepository appleRepository;
@GetMapping
Set<Apple> findAll() {
Set<Apple> apple = appleRepository.findByAppleName("Macintosh");
apple.forEach(System.out::println);
return apple;
}
}
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 java.util.Objects;
@Data
@Entity
@NoArgsConstructor
public class Apple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long appleId;
private String appleName;
private String taste;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Apple apple = (Apple) o;
return Objects.equals(appleName, apple.appleName) &&
Objects.equals(taste, apple.taste);
}
@Override
public int hashCode() {
return Objects.hash(appleName, taste);
}
}
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.Set;
@Repository
public interface AppleRepository extends JpaRepository<Apple, Long> {
Set<Apple> findByAppleName(String appleName);
}
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_.taste as taste3_0_ from apple apple0_ where apple0_.apple_name=?
Apple(appleId=1, appleName=Macintosh, taste=tangy)
Apple(appleId=3, appleName=Macintosh, taste=sweet)
Apple(appleId=5, appleName=Macintosh, taste=juicy)
Apple(appleId=7, appleName=Macintosh, taste=tart)