Home > Enterprise >  He return null, from my repository with UUID, primary id
He return null, from my repository with UUID, primary id

Time:03-25

I have one entity like this with uuid pk key...

package com.bonrefil_rest_api.bonrefil_rest_api.data.entity;

import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.util.*;

@Entity
@Table(name = "annonce")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Annonce {
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(length = 16, name = "annonce_id", nullable = false, updatable = false)
    private UUID annonce_id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String title;

    @Column(nullable = false)
    private String description;

    @Column(nullable = false)
    private double price;

    @Column(nullable = false)
    private int quantity;

    @Column(nullable = false)
    private String salesTerms;

    @JoinColumn(name = "author_client_id")
    private UUID author;

    @OneToOne(mappedBy = "annonce", optional = false)
    private AnnoncePicture picture;

    @ManyToMany
    @JoinTable(name = "annonce_carts",
            joinColumns = @JoinColumn(name = "annonce_id"),
            inverseJoinColumns = @JoinColumn(name = "cart_id"))
    private List<Cart> cartList = new ArrayList<>();

    @OneToMany(mappedBy = "conversationOnAnnonce")
    private Collection<Conversation> conversationList = new ArrayList<>();

    @ManyToMany(mappedBy = "annonceList")
    private List<Categorie> categorieList = new ArrayList<>();

    @Enumerated
    @ElementCollection(fetch = FetchType.LAZY)
    private List<State> states;

    @OneToMany(mappedBy = "noteFor")
    private Collection<Note> noteList;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "createdAt", insertable = false, columnDefinition = "DATETIME DEFAULT NOW()")
    private Date createdAt;
    @Column(name = "updateAt", nullable = true, columnDefinition = "DATETIME")
    private Date updateAt;

    @Column(nullable = false)
    private String categorie;

}

I have one restrepepository like this

@RepositoryRestResource
public interface AnnonceRepository extends JpaRepository<Annonce, UUID> {
    List<Annonce> getAllByCategorie(String categorie);
    List<Annonce> findAnnonceByAuthor(Client author);
}

One controller for retrieve one annonce with uuid like this

 @GetMapping("/{annonce_id}")
    public ResponseEntity<Optional<AnnonceDTO>> getOne(@PathVariable UUID annonce_id){
        return ResponseEntity.ok(annonceService.getOne(annonce_id));
    }

And the service for

@Override
    public Optional<AnnonceDTO> getOne(UUID annonceId) {
        if (!annonceRepository.existsById(annonceId))
            throw new ElementNotFoundException();

        return annonceRepository.findById(annonceId).map(annonceMapper::toDTO);

    }

I try on postman

http://localhost:8080/annonce/0e7b4f11-b7eb-4c83-83e3-d08955ddd7ae

But return is null, and the row is on my mysql db...

I don't have error but just null, i have looking for why, but i don't found, but there is some things more to know ?...With UUID ?...I have see to on my db, the row is blob, and i try select on db, that's working only with unhex, but on spring, UUID is fonctionnaly inside ?..

Someone can help me,

Many thanks ?..

Here is too mysql Mysql

My Postman

CodePudding user response:

I have found solutions with Query on repository....

@Query("SELECT CASE WHEN COUNT(a) > 0 THEN true ELSE false END FROM Annonce a WHERE a.annonce_id = :annonceId")     
Boolean ifExistByUuid(@Param("annonceId") UUID annonce_id);    
@Query("SELECT a FROM Annonce a WHERE a.annonce_id= ?1")     
Annonce findOneByUuid(UUID annonce_id); 

Many thank for your help....That's working fine....

  • Related