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

前端 未结 3 1074
天命终不由人
天命终不由人 2021-01-04 18:48

I am new to Hibernate. While creating a small app using it I got following exception:

Exception in thread \"main\" java.lang.IllegalArgumentException: Unknown entit

相关标签:
3条回答
  • 2021-01-04 19:05

    Depends bit about project structure, but likely by adding following to persistence.xml directly under persistence-unit element.

    <class>model.Students</class>
    <class>model.Address</class>
    

    Exactly like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" 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">
      <persistence-unit name="OneToOnePU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
        <jta-data-source>students</jta-data-source>
        <class>model.Students</class>
        <class>model.Address</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
          <property name="hibernate.hbm2ddl.auto" value="create-tables"/>
          <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
          <property name="hibernate.connection.username" value="app"/>
          <property name="hibernate.connection.password" value="app"/>
          <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>
        </properties>
      </persistence-unit>
    </persistence>
    

    By the way, why do you configure properties like hibernate.dialect in both persistence.xml and hibernate.cfg.xml?

    0 讨论(0)
  • 2021-01-04 19:23

    How do you map your entities to the db tables? You can try using @Table(name="???") annotation with @Entity for this purpose, while ??? indicates the table name in the database for this entity.

    0 讨论(0)
  • 2021-01-04 19:30

    I'm Trying this and it's working with me :

    Add @EntityScan( basePackages = {"com.yourpkghere"} to Application class. To be like this :

    @EntityScan( basePackages = {"com.yourpkghere"})
    @SpringBootApplication
    
    0 讨论(0)
提交回复
热议问题