mysqlspring-bootspring-data-jpaspring-tool-suitemodelmapper

Error creating bean while running spring boot application


Im creating a Blog Application using Spring Boot, JPA and Lombok which contains three entities for now that are Post, User and Category where Category and User are parent entities of post. After creating this post entity it is giving me an error while running this application which is -

Error creating bean with name 'postController': Unsatisfied dependency expressed through field 'postService': Error creating bean with name 'postServiceImpl': Unsatisfied dependency expressed through field 'postRepo': Error creating bean with name 'postRepo' defined in com.backendapi.blogapp.repositories.PostRepo defined in @EnableJpaRepositories declared on BlogAppApplication: Could not create query for public abstract java.util.List com.backendapi.blogapp.repositories.PostRepo.findByuser(com.backendapi.blogapp.entities.user); Reason: Failed to create query for method public abstract java.util.List com.backendapi.blogapp.repositories.PostRepo.findByuser(com.backendapi.blogapp.entities.user); No property 'user' found for type 'Post'; Did you mean 'us'

The table is getting successfully created in mySQL db. For now Iam testing it on postman only for one method that is POST where Iam facing this error. Iam using Model Mapper for mapping entity obj to dto and vice versa. My code :

POST Entity

package com.backendapi.blogapp.entities;
import java.util.Date;
import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.entities.user;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.*;

@Entity
@Table(name="post")
@Getter
@Setter
@NoArgsConstructor
public class Post {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer postId;
    
    @Column(name="post_title",length=100,nullable=false)
    private String title;
    
    @Column(length=10000)
    private String content;
    private String Imgname;
    private Date addDate;
    
    @ManyToOne
    @JoinColumn(name="category_id")
    private Category category;
    
    @ManyToOne
    private user us;
}

USER Entity**

package com.backendapi.blogapp.entities;

import java.util.ArrayList;
import java.util.List;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name="users")
@NoArgsConstructor
@Getter
@Setter
public class user {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    
    @Column(name="user_nmae", nullable=false, length=100)
    private String name;
    private String email;
    private String password;
    private String about;
    
    @OneToMany(mappedBy="us",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    private List<Post> posts=new ArrayList<>();
}

CATEGORY Entity

package com.backendapi.blogapp.entities;

import java.util.ArrayList;
import java.util.List;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name="categories")
@Getter
@Setter
@NoArgsConstructor
public class Category {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer categoryId;
    
    @Column(name="title",length=10)
    private String categoryTitle;
    
    @Column(name="description")
    private String categoryDesc;
    
    @OneToMany(mappedBy="category", cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    private List<Post> posts=new ArrayList<>();
    
    
}

POST DTO Class

package com.backendapi.blogapp.payloads;

import java.util.Date;

import com.backendapi.blogapp.entities.Category;
import com.backendapi.blogapp.entities.user;
import lombok.*;

@Getter
@Setter
@NoArgsConstructor
public class PostDto {
    
    private String title;
    private String content;
    private String Imgname;
    private Date addDate;
    private Category category;
    private user us;
    
    
}

POST REPOSITORY CLASS

package com.backendapi.blogapp.repositories;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.backendapi.blogapp.entities.Category;
import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.entities.user;

@Repository
public interface PostRepo extends JpaRepository<Post,Integer> {

    List<Post> findByuser(user us);
    List<Post> findByCategory(Category cat);
}

POST SERVICE INTERFACE

package com.backendapi.blogapp.services;

import java.util.List;

import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.payloads.PostDto;

public interface PostService {

    //create
    PostDto createPost(PostDto postDto,Integer userId,Integer categoryId);
    
    //update
    PostDto updatePost(PostDto postDto,Integer postId);
    
    //delete
    void deletePost(Integer postId);
    
    //get by postId
    PostDto getPostById(Integer postId);
    
    //get by categoryId
    PostDto getPostByCategoryId(Integer categoryId);
    
    //get by userId
    PostDto getPostByUserId(Integer userId);
    
    //getAll
    List<PostDto> getAllPost();
    
    //search post
    List<PostDto> searchPost(String keyword);
    
}

POST SERVICE Implementation class

package com.backendapi.blogapp.services.impl;

import java.util.Date;
import java.util.List;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.backendapi.blogapp.entities.Category;
import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.entities.user;
import com.backendapi.blogapp.exceptions.ResourceNotFoundException;
import com.backendapi.blogapp.payloads.PostDto;
import com.backendapi.blogapp.repositories.CategoryRepo;
import com.backendapi.blogapp.repositories.PostRepo;
import com.backendapi.blogapp.repositories.UserRepo;
import com.backendapi.blogapp.services.PostService;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostRepo postRepo;
    
    @Autowired
    private UserRepo userRepo;
    
    @Autowired
    private CategoryRepo categoryRepo;
    
    @Autowired
    private ModelMapper modelMapper;
    
    @Override
    public PostDto createPost(PostDto postDto,Integer userId,Integer categoryId) {
        
        user us=this.userRepo.findById(userId).orElseThrow(()->new ResourceNotFoundException("User","User Id",userId));
        Category cat=this.categoryRepo.findById(categoryId).orElseThrow(()->new ResourceNotFoundException("Category","Category Id",categoryId));
        
        Post post=this.dtoToPost(postDto);
        post.setImgname("D:\\My Data\\projects\\major\\Sentiment_Analysis\\practice\\Sundar Pichai\\gettyimages-174342043-612x612.jpg");
        post.setAddDate(new Date());
        post.setUs(us);
        post.setCategory(cat);
        Post savedPost=this.postRepo.save(post);
        return this.postToDto(savedPost);
    }

    @Override
    public PostDto updatePost(PostDto postDto, Integer postId) {
        
        return null;
    }

    @Override
    public void deletePost(Integer postId) {
        // TODO Auto-generated method stub

    }

    @Override
    public PostDto getPostById(Integer postId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public PostDto getPostByCategoryId(Integer categoryId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public PostDto getPostByUserId(Integer userId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<PostDto> getAllPost() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<PostDto> searchPost(String keyword) {
        // TODO Auto-generated method stub
        return null;
    }

    public PostDto postToDto(Post post) {
        PostDto pDto=this.modelMapper.map(post,PostDto.class);
        return pDto;
    }
    
    public Post dtoToPost(PostDto pDto) {
        Post post=this.modelMapper.map(pDto,Post.class);
        return post;
    }
}

POST CONTROLLER Class

package com.backendapi.blogapp.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.payloads.PostDto;
import com.backendapi.blogapp.services.PostService;

@RestController
@RequestMapping("/api")
public class PostController {
    
    @Autowired
    PostService postService;
    
    //Post
    @PostMapping("/user/{userId}/category/{categoryId}/posts")
    public ResponseEntity<PostDto> createPost(@RequestBody PostDto postDto,@PathVariable Integer userId,@PathVariable Integer categoryId){
        PostDto post=this.postService.createPost(postDto,userId,categoryId);
        return new ResponseEntity<>(post,HttpStatus.CREATED);
    }
    
    //Put
    
    //Delete
    
    //Get
    
    //GetAll
    
    //Search
}

How can I solve this error ?


Solution

  • As the error messages say:

    Failed to create query for method public abstract java.util.List com.backendapi.blogapp.repositories.PostRepo.findByuser(com.backendapi.blogapp.entities.user); No property 'user' found for type 'Post'; Did you mean 'us'

    You have no proerty user in the entity POST

    So the method must be named:

    List<Post> findByUs(user us);