0

When I run my test class called UserServiceTest I get this error. The weird thing is, I am not trying to persist an instance of the UserServiceTest type.

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.RunAfters.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.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

Here is my 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. Tables are created just fine, as I can see in the output of the build and in the 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. I did a usage search in NetBeans for any instances of UserServiceTest that I perhaps wrongfully instantiated. 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. I used the latest 4.x version of Hibernate as well as the latest 5.x and 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>
2
  • Can you please show the code of UserServiceTest Commented Apr 12, 2019 at 15:17
  • Just edited the post and included the code. Commented Apr 12, 2019 at 15:24

1 Answer 1

3

Why are instantiating the User class like this:

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

This will creates an inner class of type: UserServiceTest$1

And this class is not recognized by Hibernate as the stack trace says:

java.lang.IllegalArgumentException: Unknown entity: 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>");
Sign up to request clarification or add additional context in comments.

2 Comments

Please remove the location and website from your comment. I just edited the original post as well. its private info. Other than that, thanks very much for the solution! It works great. I was pulling my hair out. I had no idea that it would create an inner class of that type.
I removed it but in the question is still the location

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.