如何摆脱' java.lang.IllegalArgumentException:未知实体'在运行hibernate应用程序时?

时间:2016-11-27 04:08:43

标签: java mysql spring hibernate jpa

我是Hibernate的新手,在使用它创建一个小应用程序时,我遇到了异常(错误与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)

有人可以帮帮我吗?

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(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档案:

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"/>

1 个答案:

答案 0 :(得分:1)

尝试在您的实体User中添加此内容。

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

我认为找不到您的用户实体,因为它到达了休眠API Entity而不是持久性Entity