开发者

Hibernate @ManyToOne references an unknown entity

I am receiving the following Hibernate Exception:

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

The simplified Matchup class looks like this:

@Entity public class Matchup implements Serializable 
{
   protected Team awayTeam;

   @ManyToOne 
   @JoinColumn(name="away_team_id")
   public Team getAwayTeam() {
      return awayTeam;
   }
}

The simplified Team class looks like this:

@Entity
public class Team implements Serializable {
    protected List<Matchup> matchups;

    @OneToMany(mappedBy="awayTeam", targetEntity = Matchup.clas开发者_如何学JAVAs,
    fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    public List<Matchup> getMatchups() {
       return matchups;
    }
}

Notes:

  • Both Matchup and Team have subclasses. I'm not sure if this impacts the situation.
  • Both Matchup and Team are listed in my persistence.xml as being included.
  • If I put @Transient annotations on both getter methods, the error disappears.

Can anybody shed light on why this exception is occurring?


I figured out the problem: I was not adding class Team to the Hibernate AnnotationConfiguration object. Thus, Hibernate was not recognizing the class.


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


Another solution: Check to ensure that the referenced class is included your hibernate.cfg.xml file.


If you are using Spring Boot in combination with hibernate, then actually you may just need to add the package to your @EntityScan base package array.

@SpringBootApplication(scanBasePackages = {"com.domain.foo.bar.*"})
@EnableJpaRepositories(basePackages ={"com.domain.foo.bar.*"})
@EntityScan(basePackages ={"com.domain.foo.bar.*", "com.domain.another.*"})
public class SpringBootApplication extends SpringBootServletInitializer {
}


If you do not use the hibernate.cfg.xml you can add targetEntity parameter in @ManyToOne/@OneToMany annotation with class describing your entity.

For instance:

@ManyToOne(targetEntity = some.package.MyEntity.class)


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.


In my case it was persistence.xml which listed all entity classes except one...


Try to add the Qualified Name (ClassNAME), just like this:

<hibernate-configuration>
    <session-factory name="java:/hibernate/SessionFactory">
                  <mapping class="co.com.paq.ClassNAME" />
        </session-factory>
</hibernate-configuration>

In the File:

META-INF/hibernate.cfg.xml


Add the class in hibernate.cfg in proper order. First map the file that is going to be referred by another class


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


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>


In my case I had to look through all classes annotated with @Configuration. One of the classes defined an EntityManagerFactory bean as follows:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan("com.mypackage.entities");
    // omitted
}

whereas I put a new entity in the package com.mypackage.components.user.persistence. So I had to update the last line of the EntityManagerFactory bean definition to:

em.setPackagesToScan("com.mypackage.entities", "com.mypackage.components");

Also, it won't hurt to look at the @EnableJpaRepositories annotation and check its basePackages attribute as well. It might well be the case that your new entity resides outside the packages listed in the attribute's value.


I had this problem cz I didnt add "SomeClass" class with addAnnotaitedClass(SomeClass.class) to SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜