Skip to content

Commit

Permalink
Add reproducer issue as integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Nov 20, 2024
1 parent 01fb703 commit 8847019
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkus.it.spring.data.jpa.generics;

import java.util.Objects;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

import org.hibernate.Hibernate;

@Entity
public class Child extends ChildBase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o))
return false;
Child that = (Child) o;
return id != null && Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.it.spring.data.jpa.generics;

import jakarta.persistence.MappedSuperclass;

@MappedSuperclass
public class ChildBase {
String name;
String detail;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkus.it.spring.data.jpa.generics;

import java.util.Objects;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

import org.hibernate.Hibernate;

@Entity
public class Father extends FatherBase<Child> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o))
return false;
Father that = (Father) o;
return id != null && Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.it.spring.data.jpa.generics;

import java.util.List;

import jakarta.persistence.CascadeType;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.OneToMany;

@MappedSuperclass
public class FatherBase<T extends ChildBase> {
String name;
String detail;
int age;
float test;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<T> children;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.it.spring.data.jpa.generics;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface FatherBaseRepository<T extends FatherBase<?>> extends JpaRepository<T, Long> {
long countParentsByChildren_Name(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.quarkus.it.spring.data.jpa.generics;

public interface FatherRepository extends FatherBaseRepository<Father> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.it.spring.data.jpa.generics;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/rest/fathers")
@ApplicationScoped
public class FatherResource {
private final FatherBaseRepository<Father> fatherRepository;

public FatherResource(FatherBaseRepository<Father> fatherRepository) {
this.fatherRepository = fatherRepository;
}

@GET
@Path("/{childName}")
@Produces(MediaType.APPLICATION_JSON)
public long getParents(@PathParam("childName") String childName) {
long nameFathers = fatherRepository.countParentsByChildren_Name(childName);
return nameFathers;
}

}
23 changes: 23 additions & 0 deletions integration-tests/spring-data-jpa/src/main/resources/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,26 @@ INSERT INTO CatalogValue(id, key_, displayName, type) VALUES (1, 'DE-BY', 'Bavar
INSERT INTO CatalogValue(id, key_, displayName, type) VALUES (2, 'DE-SN', 'Saxony', 'federalState');
INSERT INTO CatalogValue(id, key_, displayName, type) VALUES (3, 'DE', 'Germany', 'country');
INSERT INTO CatalogValue(id, key_, displayName, type) VALUES (4, 'FR', 'France', 'country');

INSERT INTO Father (id, name, detail, age, test) VALUES (1, 'John Doe', 'Graphic designer and enjoys cooking', 40, 75.5);
INSERT INTO Father (id, name, detail, age, test) VALUES (2, 'Jane Smith', 'Software engineer and mother of two', 35, 82.3);
INSERT INTO Father (id, name, detail, age, test) VALUES (3, 'Sarah Johnson', 'She is a nurse and loves sport', 50, 92.7);

-- Insert sample data for Child associated with each Parent
INSERT INTO Child (id, name, detail) VALUES (1, 'Auri', 'Loves drawing and painting');
INSERT INTO Child (id, name, detail) VALUES (2, 'Bob', 'Plays the violin and loves animals');
INSERT INTO Child (id, name, detail) VALUES (3, 'Eve', 'Loves drawing and painting');
INSERT INTO Child (id, name, detail) VALUES (4, 'Diana', 'Plays basketball and chess');
INSERT INTO Child (id, name, detail) VALUES (5, 'Eve', 'Enjoys building robots and coding');
INSERT INTO Child (id, name, detail) VALUES (6, 'Frank', 'Writes short stories and poems');
INSERT INTO Child (id, name, detail) VALUES (7, 'Grace', 'Interested in astronomy and space');
INSERT INTO Child (id, name, detail) VALUES (8, 'Eve', 'Enjoys skateboarding and video games');

INSERT INTO father_child (father_id, children_id) VALUES (1,1);
INSERT INTO father_child (father_id, children_id) VALUES (1,2);
INSERT INTO father_child (father_id, children_id) VALUES (1,3);
INSERT INTO father_child (father_id, children_id) VALUES (2,4);
INSERT INTO father_child (father_id, children_id) VALUES (2,5);
INSERT INTO father_child (father_id, children_id) VALUES (3,6);
INSERT INTO father_child (father_id, children_id) VALUES (3,7);
INSERT INTO father_child (father_id, children_id) VALUES (3,8);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.it.spring.data.jpa.generics;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class FatherResourceIT extends FatherResourceTest {
// Execute the same tests but in packaged mode.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.it.spring.data.jpa.generics;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class FatherResourceTest {

@Test
public void shouldReturnEveParents() {
given()
.when().get("/rest/fathers/Eve")
.then()
.statusCode(200).body(containsString("3"));
}

}

0 comments on commit 8847019

Please sign in to comment.