How to resolve this 'java.lang.Illegalargumentexception: Unknown Entity' when running this simple application?

Asked

Viewed 1,673 times

4

I am new to the world of Hibernate 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)

User Entity:

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 ARCHIVE:

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>

  • 2

    Welcome to Stackoverflow in English, please translate your question.

  • Could include the imports and the definition of package in the code of its classes User and UserService?

  • Because your method createUser creates a EntityManagerFactory and destroys it instead of using the EntityManager injected?

  • @diegofm translated question!

  • @Victorstafusa I used in parts the material of this link: (https://www.caelum.com.br/apostila-java-web/uma-introducao-pratica-ao-jpa-com-hibernate/#14-10-recording-recording-and-gathering-objects). Also, when using the injected Entitymanager gives Nullpointerexception in the Userservice persist.

  • @Natanielcarvalho If the EntityManager injected gives NullPointerException, means there was no injection. In this case, either you abandon the dependency injection at once and do it all by hand, or you fix the dependency injection and do nothing at hand. What you can’t keep is a Frankenstein that starts from one thing and starts from another.

  • Anyway, your question seems to be pertinent and it doesn’t seem like a silly mistake, so take my +1 vote. I don’t know yet what the solution is.

Show 2 more comments

2 answers

4


The error is in your class User here:

import org.hibernate.annotations.Entity;

That would be right:

import javax.persistence.Entity;

And just for the record, I already had this same problem in 2010. It’s very annoying that the people from Hibernate had the "brilliant and brilliant"[1] idea of defining an annotation also called Entity.

More "brilliant and brilliant"[1] It is still Ibernate himself not to recognize it, nor to issue Warning and nothing about it.

[1]: Sarcasm

  • 2

    Man... "sensational", the Intellij should add a warning! I’ve worked using it for a long time... I came to make my playground at home and he turned into a little monster, even bizarre this wonderful import...

2

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

Try it like this:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
  • Hello, I defined the id generation strategy and the problem persists.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.