Nataniel Carvalho Nataniel Carvalho - 6 days ago 5
MySQL Question

How to get rid of 'java.lang.IllegalArgumentException: Unknown entity' while running a hibernate app?

I am new to Hibernate and while creating a small app using it I got following exception (error is related with persist of EntityManager):

java.lang.IllegalArgumentException: Unknown entity: com.nataniel.api.domain.User
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:840)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at com.sun.proxy.$Proxy993.persist(Unknown Source)
at com.nataniel.api.services.UserService.createUser(UserService.java:29)


Can anybody please help me out?

The Entity class is as follows:

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(strategy=GenerationType.IDENTITY)
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"));
entityManager.persist(user);

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_1_0.xsd"
version="1.0">
<persistence-unit name="service-provider" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.nataniel.api.domain.User</class>

<properties>
<!-- 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>
</persistence>


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!-- CXF -->
<cxf:rsServer id="user" address="/userservice"
serviceClass="com.nataniel.api.UserServiceRest"></cxf:rsServer>

<context:component-scan base-package="com.nataniel"/>

<!-- Camel -->
<bean id="routeBuilder" class="com.nataniel.api.camel.MailServiceRouteBuilder"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="routeBuilder"/>
</camelContext>


<!-- Persistence -->
<bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.0.140:3306/service-provider"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="mysqlDataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="mysqlDataSource"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="false"/>



Answer

try to add this in your Entity User.

@Entity
@javax.persistence.Entity; // add this <--
@Table(name="USER")

I think your User Entity is not located because it reaches the hibernate API Entity and not the persistence Entity.

Comments