Home > Software design >  jpa repositofy findall() returns empty list
jpa repositofy findall() returns empty list

Time:01-11

I'm practicing making web pages using spring boot.

I created an h2 DB and connected it, and I want to show the name properties of the members table as a list on my web page.

I created the findall() method, but only an empty list is returned. What's wrong with my code?

my web page

MemberRepository

package com.example.testproject.store.h2.repository;

import com.example.testproject.store.h2.domain.Members;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MemberRepository extends JpaRepository<Members, Integer> {

}

MemberService

package com.example.testproject.store.h2.service;

import com.example.testproject.store.h2.domain.Members;

import java.util.List;

public interface MemberService {
    List<Members> getAll();

}

MemberServiceImpl

package com.example.testproject.store.h2.service;

import com.example.testproject.store.h2.domain.Members;
import com.example.testproject.store.h2.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Member;
import java.util.List;

@Service(value = "memberServiceImpl")
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberRepository memberRepository;

    public List<Members> getAll(){
        return memberRepository.findAll();
    }

}

MemberController

package com.example.testproject.controller;

import com.example.testproject.store.h2.domain.Members;
import com.example.testproject.store.h2.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
public class MemberController {
    @Autowired
    private MemberService memberService;
    @GetMapping(value = "/members")
    public List<Members> getAll() {
        List<Members> users = memberService.getAll();
        return users;
    }

    /*@GetMapping(value = "/members")
    public List<Members> getAll() throws Exception {
        return memberService.getAll();
    }*/

}

Members(Entity)

package com.example.testproject.store.h2.domain;

import jakarta.persistence.*;

@Entity
@Table(name = "members")
public class Members {

    @Id
    private int id;

    @Column
    private String name;

}

I want to show the name properties of the members table as a list on my web page.

CodePudding user response:

I think you missed getters and setters in your Entity Class just add them by using @Data annotation and try again.

CodePudding user response:

You have missed the getter setter method in entity class either you need to 1:-define getter setter method for each field or 2:-you need to add lombook dependency in pom.xml file and add @Data annootation on top of entity class so that when spring uses your entity can set and get the value from database and if you dontot want to give id explicitly then add @GeneratedValue(strategy=GenerationType.AUTO) so that spring automatically increase you id

CodePudding user response:

You missed getters and setters for the fields in your Entity

You do not need any library, you can write it by yourself.

Otherwise, even if you use lombok, you should avoid to use @Data annotation on Entity, because it could lead to errors with default implementation of equals and hashCode. Instead you can use annotations @Getter and @Setter on the class.

  • Related