Kévin
Kévin

Reputation: 557

No property found SpringBoot JPA

I encouter this error while running the app, i dont understand why.. i found information on the web, but can't understand what's wrong. Thanks for help.

2 ) Another question regarding this, should i put :

List<HistoriqueDeploiement> findByIdNamespaceAndIdService(Namespace id_namespace, Service id_service);
Or
List<HistoriqueDeploiement> findByIdNamespaceAndIdService(Integer id_namespace, Integer id_service);

The error :

Error creating bean with name 'checkConfigDeploiementRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.jpa.repository.CheckConfigDeploiementRepository.findByIdNamespaceAndIdService(com.example.jpa.model.Namespace,com.example.jpa.model.Service)! No property namespace found for type Integer! Traversed path: CheckConfigDeploiement.id.

The Entity :

@Entity
@Table(name = "historiquedeploiement")
@Data
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
@AllArgsConstructor
public class HistoriqueDeploiement extends AuditModel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id", nullable=false, unique=true)
    private Integer id;    

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "id_namespace", nullable = false)    
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    @JsonProperty("id_namespace")
    private Namespace namespace;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "id_service", nullable = false)    
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    @JsonProperty("id_service")
    private Service service;

    @NotEmpty(message = "Le GitCommit ne peut être vide")
    @Size(max = 255)
    private String gitCommit;   
    
    @NotEmpty(message = "Le TagVersion ne peut être vide")
    @Size(max = 100)
    private String tagVersion;  
    
    @NotEmpty(message = "Le Actionby ne peut être vide")
    @Size(max = 255)
    private String actionBy;    
    
}

NamespaceEntity ( same with service..)

@Entity
@Table(name = "namespace")
@Data
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
@AllArgsConstructor
public class Namespace extends AuditModel {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id", nullable=false, unique=true)
    private Integer id;

    @NotEmpty
    @Size(max = 100)
    @Column(unique = true)
    private String namespace;

    @OneToMany(mappedBy = "namespace", cascade = CascadeType.ALL, orphanRemoval = true)  
    private List<HistoriqueDeploiement> historiquedeploiements = new ArrayList<>();

    public void addHistoriqueDeploiement(HistoriqueDeploiement historiquedeploiement) {
        historiquedeploiements.add(historiquedeploiement);
        historiquedeploiement.setNamespace(this);
    }

    public void removeHistoriqueDeploiement(HistoriqueDeploiement historiquedeploiement) {
        historiquedeploiements.remove(historiquedeploiement);
        historiquedeploiement.setNamespace(null);
    }
}

The repo, i don't understand what i'm doing wrong :

...
@Repository
public interface HistoriqueDeploiementRepository extends JpaRepository<HistoriqueDeploiement, Integer> {        
    List<HistoriqueDeploiement> findAll();
    
    List<HistoriqueDeploiement> findByIdNamespace(Integer id);
    
    List<HistoriqueDeploiement> findByIdNamespaceAndIdService(Namespace id_namespace, Service id_service);
    
    List<HistoriqueDeploiement> findByIdNamespaceAndLogCreatedAtBetween(Namespace id_namespace, Date datedebut, Date datefin);

    List<HistoriqueDeploiement> findByIdNamespaceAndLogCreatedAt(Namespace id_namespace, Date date);

}

Upvotes: 1

Views: 74

Answers (1)

TheCoolDrop
TheCoolDrop

Reputation: 1066

Okay so I looked at your problem and here is what I found. The types you assigned to your repository interface method parameters are wrong.

You are looking to obtain a list of HistoriqueDeploiement entities whose Namespace and Service entities have specific IDs. Note that IDs of Namespace and Service entities are Integer types. So in order to solve you can simply rewrite your methods as follows:

@Repository
public interface HistoriqueDeploiementRepository extends 
JpaRepository<HistoriqueDeploiement, Integer> {        
    List<HistoriqueDeploiement> findAll();

    List<HistoriqueDeploiement> findByNamespaceId(Integer id);

    List<HistoriqueDeploiement> findByNamespaceIdAndServiceId(Integer id_namespace, Integer id_service);

    List<HistoriqueDeploiement> findByNamespaceIdAndLogCreatedAtBetween(Integer id_namespace, Date datedebut, Date datefin);

    List<HistoriqueDeploiement> findByNamespaceIdAndLogCreatedAt(Integer id_namespace, Date date);
}

Note that major change here is that we replaced Namespace and Service types with Integer type, which is the actual type of their IDs

Upvotes: 1

Related Questions