简体   繁体   English

JPA Hibernate-排除类上的未知实体

[英]JPA Hibernate - Unknown Entity on excluded class

When I run my test class called UserServiceTest I get this error. 当我运行名为UserServiceTest的测试类时,出现此错误。 The weird thing is, I am not trying to persist an instance of the UserServiceTest type. 奇怪的是,我不是要持久保存UserServiceTest类型的实例。

The code which it fails on. 它失败的代码。 If I comment out this code the rest of the tests run and I dont get this error. 如果我注释掉此代码,其余的测试将运行,并且不会出现此错误。

User persistedUser = new User() {
    {
        setUsername("Jan123");
        setName("Jan");
        setText("test bio");
        setLocation("<location>");
        setWebsite("<website>");
    }
};

entityManager.getTransaction().begin();
entityManager.persist(persistedUser);
entityManager.getTransaction().commit();

java.lang.IllegalArgumentException: Unknown entity: UserServiceTest$1 at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:808) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) at UserServiceTest.setUp(UserServiceTest.java:55) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.internal.runners.statements.RunAf java.lang.IllegalArgumentException:未知实体:org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:808)的org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)的UserServiceTest $ 1(UserServiceTest.setUp( UserServiceTest.java:55),位于java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本地方法),位于java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62),位于java.base /位于org.junit.runners.model.FrameworkMethod $ 1.java.java / java.lang.reflect.Method.invoke(Method.java:566)的jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) (FrameworkMethod.java:50)在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)在org。 org.junit.internal.runners.statements.RunAf上的junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) ters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org. org上org.junit.runners.ParentRunner.run(ParentRunner.java:363)上的ters.evaluate(RunAfters.java:27)org上的org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)上的ters.evaluate(RunAfters.java:27) org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)上的.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)在java.base / jdk.internal.reflect java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)上的.NativeMethodAccessorImpl.invoke0(java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)上的.NativeMethodAccessorImpl.invoke0(本机方法)位于org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)的java.base / java.lang.reflect.Method.invoke(Method.java:566) org.org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)处的.booter.ProviderFactory $ ProviderProxy.invoke(ProviderFactory.java:165)。 apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)位于org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

Here is my persistence.xml. 这是我的persistence.xml。 I checked that it is used by malforming the XML on purpose to see if it throws an XML parsing error, which it does. 我检查了是否故意使XML格式错误,以查看它是否引发XML解析错误。 Tables are created just fine, as I can see in the output of the build and in the SQL Server Management Studio. 正如我在构建输出和SQL Server Management Studio中所看到的那样,表创建得很好。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="KwetterPUTest" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>Kweet</class>
    <class>User</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.connection.url" value="jdbc:sqlserver://localhost;databaseName=KwetterTest;" />
      <property name="hibernate.connection.username" value="sa" />
      <property name="hibernate.connection.password" value="admin" />
      <property name="hibernate.show_sql" value="true"/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

Edit: Here is the relevant code from the UserServiceTest. 编辑:这是UserServiceTest中的相关代码。 I did a usage search in NetBeans for any instances of UserServiceTest that I perhaps wrongfully instantiated. 我在NetBeans中进行了用法搜索,以查找可能错误地实例化的UserServiceTest的任何实例。 There are none. 没有了。

private static EntityManagerFactory entityManagerFactory;
    private static EntityManager entityManager;
    private static UserService userService;
    private static User persistedUser;

    @BeforeClass
    public static void setUp() {
        entityManagerFactory = Persistence.createEntityManagerFactory("KwetterPUTest");
        entityManager = entityManagerFactory.createEntityManager();
        userService = new UserService(new UserDAOJPAImpl(entityManager));
        persistedUser = new User() {
            {
                setUsername("Jan123");
                setName("Jan");
                setText("test bio");
                setLocation("<location>");
                setWebsite("<website>");
            }
        };

        entityManager.getTransaction().begin();
        entityManager.persist(persistedUser);
        entityManager.getTransaction().commit();
    }

Edit 2: pom file for those who are interested. 编辑2:感兴趣的人的pom文件。 I used the latest 4.x version of Hibernate as well as the latest 5.x and 5.2.2. 我使用了最新的4.x版本的Hibernate以及最新的5.x和5.2.2。 None changed anything. 没有任何改变。

 <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.2.Final</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>7.2.1.jre8</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>8.0</version> <type>jar</type> </dependency> </dependencies> 

Why are instantiating the User class like this: 为什么要这样实例化User类:

persistedUser = new User() {
        {
            setUsername("Jan123");
            setName("Jan");
            setText("test bio");
            setLocation("<location>");
            setWebsite("<website>");
        }
    };

This will creates an inner class of type: UserServiceTest$1 这将创建一个内部类型: UserServiceTest$1

And this class is not recognized by Hibernate as the stack trace says: Hibernate无法识别此类,因为堆栈跟踪显示:

java.lang.IllegalArgumentException: Unknown entity: UserServiceTest$1 java.lang.IllegalArgumentException:未知实体:UserServiceTest $ 1

You must do it like this: 您必须这样做:

persistedUser = new User();
persistedUser.setUsername("Jan123");
persistedUser.setName("Jan");
persistedUser.setText("test bio");
persistedUser.setLocation("<location>");
persistedUser.setWebsite("<website>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM