How to resolve this 'java.lang.IllegalArgumentException: Unknown entity' when running this simple application?

4

I'm new to the Hibernate world and when I run this application, I get the following exception:

 java.lang.IllegalArgumentException: Unknown entity: com.nataniel.api.domain.User
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:840)
    at com.nataniel.api.services.UserService.createUser(UserService.java:44)

Entity User:

package com.nataniel.api.domain;

import org.hibernate.annotations.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="USER")
public class User {
    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "LOGIN")
    private String login;

    @Column(name = "NAME")
    private String name;

    @Column(name = "EMAIL")
    private String email;

    @Column(name = "PASSWORD")
    private String password;

    @Column(name = "CITY")
    private String city;

    @Column(name = "REGION")
    private String region;

    @Column(name = "BIRTHDATE")
    private String birthDate;

    public User() {
    }

    public User(String login, String name, String email, String password, String city, String region, String birthDate) {
        this.login = login;
        this.name = name;
        this.email = email;
        this.password = password;
        this.city = city;
        this.region = region;
        this.birthDate = birthDate;
    }

    // getters and setters
}

DAO file:

package com.nataniel.api.services;

import org.apache.camel.Exchange;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import com.nataniel.api.domain.User;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
@Service("userService")
public class UserService {

    @PersistenceContext
    transient EntityManager entityManager;

    @Transactional
    public String createUser(Exchange exchange) {
        JSONObject userAccountJSON = (JSONObject) exchange.getIn().getHeader("jsonRequest");

        User user = new User();
        user.setLogin(userAccountJSON.getString("login"));
        user.setEmail(userAccountJSON.getString("email"));
        user.setPassword(userAccountJSON.getString("password"));
        user.setName(userAccountJSON.getString("name"));
        user.setCity(userAccountJSON.getString("city"));
        user.setRegion(userAccountJSON.getString("region"));
        user.setBirthDate(userAccountJSON.getString("birthdate"));

        EntityManagerFactory factory = Persistence.createEntityManagerFactory("service-provider");
        entityManager = factory.createEntityManager();
        entityManager.persist(user);
        entityManager.close();

        return userAccountJSON.toString();
    }
}

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">

<persistence-unit name="service-provider">

    <!-- provedor/implementacao do JPA -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <!-- entidade mapeada -->
    <class>com.nataniel.api.domain.User</class>

    <properties>
        <!-- dados da conexao -->
        <property name="javax.persistence.jdbc.driver"
                  value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url"
                  value="jdbc:mysql://192.168.0.140:3306/service-provider" />
        <property name="javax.persistence.jdbc.user"
                  value="root" />
        <property name="javax.persistence.jdbc.password"
                  value="123" />

        <!--  propriedades do hibernate -->
        <property name="hibernate.dialect"
                  value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />

        <!--  atualiza o banco, gera as tabelas se for preciso -->
        <property name="hibernate.hbm2ddl.auto" value="update" />

    </properties>
</persistence-unit>

    
asked by anonymous 27.11.2016 / 02:42

2 answers

4

The error is in your User class here:

import org.hibernate.annotations.Entity;

The correct would be this:

import javax.persistence.Entity;

And for the record, I've had that same problem in 2010. It's annoying that the hibernate folks have had the "brilliant " [1] idea define an annotation also called Entity .

More " brilliant " [1] is still hibernate itself not recognizing it, issuing warning, I respect that.

[1]: Sarcasm

    
27.11.2016 / 05:50
2

Hello, I believe that the problem is in the id generation strategy that in the entity is not defined.

Try this:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
    
27.11.2016 / 04:19