Hibernate @ManyToOne references an unknown entity

前端 未结 12 1296
名媛妹妹
名媛妹妹 2021-01-03 17:54

I am receiving the following Hibernate Exception:

@OneToOne or @ManyToOne on Matchup.awayTeam references an unknown entity: Team

The simplified

相关标签:
12条回答
  • 2021-01-03 18:05

    I had the same problem and I was struggling with it from last couple of hours. I finally found out that the value in packageToscan property and the actual package name had case mismatch. The package was in upper case(DAO) and the packageToscan had dao as its value. just wanted to add this in case some one find it help full

    0 讨论(0)
  • 2021-01-03 18:08

    I work in a project using Spring and Hibernate 4 and I found out that we do not have a hibernate.cfg.xml file. Instead, our beans are listed in the file applicationContext.xml which looks a bit like

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.package.Bean</value>
            </list>
        </property>
    </bean>
    

    Adding my bean to the list solved the problem. You can find some more information here.

    0 讨论(0)
  • 2021-01-03 18:10

    The unknown entity error is not a hibernate annotation problem, but rather that the entity is NOT being recognized. You should check your persistence settings, whether you are using pure JPA, Hibernate or Spring.

    By default, Hibernate is capable of finding the JPA entity classes based on the presence of the @Entity annotation, so you don't need to declare the entity classes.

    With Spring you would have an @Bean similar to the following to make sure you don't have unknown entities in your package.

    @Bean
    open fun entityManagerFactory() : 
    LocalContainerEntityManagerFactoryBean {
        val em = LocalContainerEntityManagerFactoryBean()
        em.setPackagesToScan("com.package.domain.entity")
    
        //...
    }
    

    GL

    Source

    0 讨论(0)
  • 2021-01-03 18:12

    Add the Entity in all persistence-units of your persistence.xml

    Example

    <?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="SystemDataBaseDS" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/SystemDataBaseDS</jta-data-source>
        <class>package.EntityClass</class>
        ...
      </persistence-unit>
      <persistence-unit name="SystemDataBaseJDBC" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>package.EntityClass</class>
        ...
      </persistence-unit>
    </persistence>
    
    0 讨论(0)
  • 2021-01-03 18:22

    Along with entry in hibernate.cfg.xml, you'll need @Entity annotation on referenced class.

    0 讨论(0)
  • In my case it was persistence.xml which listed all entity classes except one...

    0 讨论(0)
提交回复
热议问题