简体   繁体   中英

Make CRUD in JPA using generic classes in Spring Boot

I am trying to make CRUD in JPA using generic classes in Spring Boot. But when I try to run program it is giving below errors.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityRepo': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object 
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object

EntityController.java

@Autowired
    private EntityService<Myuser, Long> userService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public void saveEntity() {
        Myuser user = new Myuser("pawam");
        userService.saveEntity(user);

    }

EntityService

public interface EntityService<T, ID> {
    public T saveEntity(T t);
}

Myuser

@Entity
@Table(name = "myuser")
public class Myuser {

    private Long id;
    private String name;

    public Myuser() {
        super();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Myuser(String name) {
        super();
        this.name = name;
    }

}

EntityRepo.java

@Repository
public interface EntityRepo<T, ID> extends CrudRepository<T, ID> {
}

EntityServiceImpl.java

@Service
public class EntityServiceImpl<T, ID> implements EntityService<T, ID> {

    @Autowired
    private EntityRepo<T, ID> repo;

    @Override
    public T saveEntity(T t) {
        return repo.save(t);
    }
}

application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=12345
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true

I tried to change @Repository to @NoRepositoryBean inside EntityRepo.java. But this also did not work. What can I do to fix this problem? Any help is appreciated.

Step 1: Change EntityService

public interface EntityService {
        <S> S saveCustom(S entity);
}

Step 2: Change EntityServiceImpl as Repository and implement save method

@Repository
public class EntityServiceImpl implements EntityService {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional 
    public <S> S saveCustom(S entity) {
        entityManager.persist(entity);
        return entity;
    }
}

Step 3: Remove @Repository from EntityRepo

public interface EntityRepo  extends JpaRepository<Myuser, Long> ,EntityService {   
}

Step 4: Change controller

@Autowired
private EntityRepo userService;

now you can save any entity type using

userService.saveCustom(XYZ);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM