2016-11-27 11 views
-3

私は休止状態に新しいですし、それを使用して小さなアプリケーションを作成しながら、私は、例外(エラーがEntityManagerのの存続に関連している)次しまっ:Hibernateアプリケーションを実行中に 'java.lang.IllegalArgumentException:Unknown entity'を取り除くには?

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

persistence.xmlの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"/> 

+0

ここで、あなたのユーザをpersistence.xmlにマップしますか? – msagala25

答えて

0

エンティティUserでこれを追加してみてください。

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

私はそれが休止状態API Entityではなく永続Entityに達するので、あなたのユーザエンティティが配置されていないと思います。

+0

Intellijは私のような人にアドバイスをすることができます! '@ javax.persistence.Entity; 'の代わりに' import org.hibernate.annotations.Entity; 'をインポートする際のエラーは、IDE、log4j、Tomcatのログなど、任意の形式で表示する必要がありました。 –

+0

Eclipse IDEでコードを作成してみてください。 – msagala25

関連する問題