Issue
I'm learning java spring boot which is generic pattern. now i found an error i've been looking for but couldn't find a crux of the problem.
when I do a GET in postman , it shows error 500 :
"Not an entity: class com.example.serverside.models.baseEntity.BaseEntity; nested exception is java.lang.IllegalArgumentException: Not an entity: class com.example.serverside.models.baseEntity.BaseEntity",
I've checked it a few times and have looked at similar questions but can't find an answer.
Here's my code on BaseEntity, GenericRepository, RegionService and RegionController.
Model BaseEntity `
package com.example.serverside.models.baseEntity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
`
Model Region
`
package com.example.serverside.models;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.example.serverside.models.baseEntity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="region")
public class Region extends BaseEntity {
@Column( nullable = false)
private String name;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@OneToMany(mappedBy = "region", cascade = CascadeType.ALL)
private List<Country> countries;
}
`
GenericRepository
`
package com.example.serverside.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.serverside.models.baseEntity.BaseEntity;
@Repository
public interface GenericRepository<T extends BaseEntity> extends JpaRepository<T,Long> {
}
`
RegionService
`
package com.example.serverside.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.serverside.models.Region;
import com.example.serverside.repository.GenericRepository;
@Service
public class RegionService {
private GenericRepository<Region> regionRepository;
@Autowired
public RegionService(GenericRepository<Region> regionRepository) {
this.regionRepository = regionRepository;
}
// findAll
public List<Region> findAll(){
return regionRepository.findAll();
}
}
`
RegionController
`
package com.example.serverside.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.serverside.models.Region;
import com.example.serverside.services.RegionService;
import lombok.AllArgsConstructor;
@RestController
@RequestMapping("/api/v1/region")
public class RegionController{
private RegionService regionService;
@Autowired
public RegionController(RegionService regionService) {
this.regionService = regionService;
}
@GetMapping
public ResponseEntity<List<Region>> findAll() {
return new ResponseEntity(regionService.findAll(), HttpStatus.OK);
}
}
`
I hope to find the crux of my problem, and how to solve it
Solution
public interface GenericRepository<T extends BaseEntity> extends JpaRepository<T,Long>
what is the purpose of creating a generic repository? BaseEntity is only a model which is extended by the real entities like Region. See that it has no @Entity annotation.
So, if you are storing region in database define the repository for Region.
public interface RegionRepository extends JpaRepository<Region, Long>
Answered By - sanurah
Answer Checked By - Terry (JavaFixing Volunteer)