Skip to content

Commit

Permalink
Test embedded id
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Jan 30, 2025
1 parent 1981cd8 commit c196d84
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
package org.acme.spring.data.jpa;

import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;

@Entity
@IdClass( Fruit.FruitId.class )
public class Fruit {

@Id
@GeneratedValue
private Long id;

@Id
private String name;
@EmbeddedId
private FruitId id;

private String color;

public Fruit() {
}

public Fruit(String name, String color) {
this.name = name;
this.id = new FruitId( name );
this.color = color;
}

public Long getId() {
public FruitId getId() {
return id;
}

public void setId(Long id) {
public void setId(FruitId id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
return id.getName();
}

public String getColor() {
Expand All @@ -50,13 +43,19 @@ public void setColor(String color) {
this.color = color;
}

@Embeddable
public static class FruitId {
@GeneratedValue
private Long id;
private String name;

private FruitId(){
}

public FruitId(String country) {
this.name = country;
}

public FruitId(Long id, String country) {
this.id = id;
this.name = country;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface FruitRepository extends CrudRepository<Fruit, Long> {
public interface FruitRepository extends CrudRepository<Fruit, Fruit.FruitId> {

List<Fruit> findByColor(String color);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public Iterable<Fruit> findAll() {
}

@DELETE
@Path("{id}")
public void delete(long id) {
fruitRepository.deleteById(id);
@Path("id/{id}/name/{name}")
public void delete(long id, String name) {
fruitRepository.deleteById(new Fruit.FruitId( id, name ));
}

@POST
Expand All @@ -39,10 +39,10 @@ public Fruit create(String name, String color) {
}

@PUT
@Path("/id/{id}/color/{color}")
@Path("/id/{id}/name/{name}/color/{color}")
@Produces("application/json")
public Fruit changeColor(Long id, String color) {
Optional<Fruit> optional = fruitRepository.findById(id);
public Fruit changeColor(Long id, String name, String color) {
Optional<Fruit> optional = fruitRepository.findById( new Fruit.FruitId( id, name ) );
if (optional.isPresent()) {
Fruit fruit = optional.get();
fruit.setColor(color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void testListAllFruits() {

//Delete the Cherry:
given()
.when().delete("/fruits/1")
.when().delete("/fruits/id/1/name/Cherry")
.then()
.statusCode(204);

Expand Down Expand Up @@ -89,7 +89,7 @@ void testFindByColor() {

//Update color of Avocado
given()
.when().put("/fruits/id/4/color/Black")
.when().put("/fruits/id/4/name/Avocado/color/Black")
.then()
.statusCode(200)
.body(containsString("Black"));
Expand Down

0 comments on commit c196d84

Please sign in to comment.