2

Actualmente tengo un problema al hacer un método en JPA, la respuesta se duplica cuando hago una query a una entidad que contiene 2 listas.

Entidad.

@Entity
@Table(name = "inscription")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Inscription {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private int registryNumber;

    private int pageNumber;

    private String address;

    private String year;

    private String roleProperty;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE, targetEntity = Nature.class)
    @JoinColumn(name = "id_nature")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Nature nature;

    @OneToMany(targetEntity = Person.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private List<Person> firstPerson;

    @OneToMany(targetEntity = Person.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private List<Person> secondPerson;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE, targetEntity = User.class)
    @JoinColumn(name = "id_user")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private User registeredUser;

}

Entidad Persona

@Entity
@Table(name = "person")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String fullName;

    private String dni;


}

Repositorio

@Repository
public interface InscriptionRepository extends JpaRepository<Inscription, Long> {

    Page<Inscription> findAll(Pageable pageable);

    Page<Inscription> findAllByRoleProperty(Pageable pageable, String roleProperty);

    Page<Inscription> findAllByFirstPersonDniContainsOrSecondPersonDniContains(Pageable pageable, String dni, String dni2);

    Page<Inscription> findAllByFirstPersonFullNameContainsOrSecondPersonFullNameContains(Pageable pageable, String fullname, String fullname2);

    //Page<Inscription> findAllByFirstPersonFullNameContains(Pageable pageable, String fullname);
    //Page<Inscription> findAllBySecondPersonFullNameContains(Pageable pageable, String fullname);


}

Cuando llamo al repository

         case "TEST2":
                    inscriptionPage = inscriptionRepository.findAllByFirstPersonFullNameContainsOrSecondPersonFullNameContains(paging, filter, filter);
                    break;

Esta respuesta obtengo

introducir la descripción de la imagen aquí

Dejo el JSON de respuesta donde se aprecian los elementos duplicados.

{
    "inscriptionList": [
        {
            "id": 23,
            "registryNumber": 1,
            "pageNumber": 2,
            "address": "Los tineos 140",
            "year": "2022",
            "roleProperty": "SASD-123",
            "nature": {
                "id": 3,
                "nature": "Edit3",
                "nameFirstPerson": "Primera Persona",
                "nameSecondPerson": "Segunda Persona"
            },
            "firstPerson": [
                {
                    "id": 66,
                    "fullName": "bbb",
                    "dni": "01"
                },
                {
                    "id": 67,
                    "fullName": "ccc",
                    "dni": "02"
                }
            ],
            "secondPerson": [
                {
                    "id": 68,
                    "fullName": "ddd",
                    "dni": "03"
                }
            ],
            "registeredUser": {
                "id": 1,
                "username": "jorge",
                "email": "[email protected]",
                "statusActive": true,
                "roles": [
                    {
                        "id": 3,
                        "name": "ROLE_ADMIN"
                    }
                ]
            }
        },
        {
            "id": 23,
            "registryNumber": 1,
            "pageNumber": 2,
            "address": "Los tineos 140",
            "year": "2022",
            "roleProperty": "SASD-123",
            "nature": {
                "id": 3,
                "nature": "Edit3",
                "nameFirstPerson": "Primera Persona",
                "nameSecondPerson": "Segunda Persona"
            },
            "firstPerson": [
                {
                    "id": 66,
                    "fullName": "bbb",
                    "dni": "01"
                },
                {
                    "id": 67,
                    "fullName": "ccc",
                    "dni": "02"
                }
            ],
            "secondPerson": [
                {
                    "id": 68,
                    "fullName": "ddd",
                    "dni": "03"
                }
            ],
            "registeredUser": {
                "id": 1,
                "username": "jorge",
                "email": "[email protected]",
                "statusActive": true,
                "roles": [
                    {
                        "id": 3,
                        "name": "ROLE_ADMIN"
                    }
                ]
            }
        }
    ],
    "currentPage": 1,
    "totalItems": 2,
    "totalPages": 1
}

Esto me pasa cuando la búsqueda que hago se encuentra en el atributo secondPerson de la entidad(Le pase un fullName = "bbb"), ya que cuando hago esta misma búsqueda y el fullName lo encuentra en firstPerson responde solo un elemento.

Saben que podría estar ocurriendo?

1
  • Encontraste alguna solucion? Tengo justo el mismo problema el 20 jul. 2023 a las 11:46

1 respuesta 1

0

Parece que no te esta cogiendo el segundo parametro en tu metodo. Probaría a cambiar el query method por otro nombre como por ejemplo:

Page<Inscription> findAllByFirstPersonFullNameContainsOrSecondPersonDniContains(Pageable pageable, String fullname, String dni);

si el problema esta en como detecta el query method los parametros lo mejor seria escribirlo como un @Query.

1
  • Si lo toma, ya que cuando quiero hacer la búsqueda por "secondPerson" me trae registros duplicados el 11 ago. 2022 a las 1:05

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.