2

I am trying to create small String boot application with Hibernate. I getting below error when tried to save object(User)

java.lang.IllegalArgumentException: Unknown entity: com.prithvi.thoughtprocess.beans.dao.impl.User
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_51]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_51]

I searched this on google but did not find solution. Below is my User entity -

package com.prithvi.thoughtprocess.beans.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class User  implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 3367865397662004005L;

    @Id
    @GeneratedValue(strategy =  GenerationType.SEQUENCE)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    }

UserDAOImpl class is as below -

package com.prithvi.thoughtprocess.beans.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.prithvi.thoughtprocess.beans.dao.UserDAO;
import com.prithvi.thoughtprocess.beans.domain.User;

@Repository
public class UserDAOImpl  implements UserDAO{

    @PersistenceContext 
    private EntityManager entityManager;    

    @Override
    public <S extends User> S save(S entity) {
        entityManager.persist(entity);
        return null;
    }}

application.properties file

spring.datasource.url=jdbc:mysql://localhost:3306/bookbank
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE 

Below is the class which contains main method -

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.prithvi.thoughtprocess.dto.BookDto;

@ComponentScan(basePackages = {"com.prithvi.thoughtprocess.beans"})
@SpringBootApplication
public class Example {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

Controller class -

package com.prithvi.thoughtprocess.beans.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.prithvi.thoughtprocess.beans.service.UserService;
import com.prithvi.thoughtprocess.dto.UserDto;

@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = "user/save", method = RequestMethod.POST)
    String save(@RequestBody UserDto userDto) {
        userService.saveUser(userDto);
        return "User save successfully"; 
    }

    @RequestMapping(value = "user/list", method = RequestMethod.GET)
    List<UserDto> list() {
        return userService.listUser(); 
    }

}

Service class which calls DAO layer -

package com.prithvi.thoughtprocess.beans.service.impl;

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


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.prithvi.thoughtprocess.beans.dao.UserDAO;
import com.prithvi.thoughtprocess.beans.domain.User;
import com.prithvi.thoughtprocess.beans.service.UserService;
import com.prithvi.thoughtprocess.dto.UserDto;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO; 

    @Override

    public void saveUser(UserDto userDto) {
        userDAO.save(convertToEntity(userDto));
    }

    public User convertToEntity(UserDto userDto){
        User user = new User();
        user.setId(userDto.getId());
        user.setName(userDto.getName());
        return user;
    }

    public UserDto convertToDto(User user){
        UserDto userDto = new UserDto();
        userDto.setId(user.getId());
        userDto.setName(user.getName());
        return userDto;
    }

    @Override
    public List<UserDto> listUser() {
        //List<User> users = userDAO.list();
        List<UserDto> userDtos = new ArrayList<>();
        /*for (User user : users) {
            userDtos.add(convertToDto(user));
        }*/
        return userDtos;
    }

}
5
  • 2
    try adding @EntityScan( basePackages = {"com.prithvi.thoughtprocess.beans"} to the application class Commented Jul 19, 2017 at 12:50
  • Your message complains about an entity "com.prithvi.thoughtprocess.beans.dao.impl.User". The entity you posted is in the package "com.prithvi.thoughtprocess.beans.domain.User". Use the right package name. Commented Jul 19, 2017 at 12:53
  • 1
    @JB still getting same error - java.lang.IllegalArgumentException: Unknown entity: com.prithvi.thoughtprocess.beans.domain.User at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_51] Commented Jul 19, 2017 at 13:08
  • What is the package of your Example class? Don't put it in the default package, and make sure the entities are in a sub-package of the Example class package. See docs.spring.io/spring-boot/docs/current/reference/htmlsingle/… and docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…. Commented Jul 19, 2017 at 13:17
  • @Jens Thanks.. your solution worked. thanks a lot Commented Jul 20, 2017 at 8:14

1 Answer 1

8

Adding @EntityScan( basePackages = {"com.prithvi.thoughtprocess.beans"} to your Application class. That is needed for hibernate to scan your entities.

Sign up to request clarification or add additional context in comments.

1 Comment

Quick question - "EntityScan" belongs to Spring, how does it solves the Hibernate issue? is there any contract? Any other way to fix it if i don't want to include spring dependencies?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.