Reputation: 1483
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.class,
fetch=FetchType.EAGER, cascade=CascadeType.ALL)
public List<Matchup> getMatchups() {
return matchups;
}
}
Notes:
Can anybody shed light on why this exception is occurring?
Upvotes: 55
Views: 107781
Reputation: 1
I had this problem cz I didnt add "SomeClass" class with addAnnotaitedClass(SomeClass.class)
to SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").
Upvotes: 0
Reputation: 996
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.
Upvotes: 0
Reputation: 4726
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>
Upvotes: 1
Reputation: 22867
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
Upvotes: -1
Reputation: 5372
In my case it was persistence.xml
which listed all entity classes except one...
Upvotes: 1
Reputation: 24740
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 {
}
Upvotes: 10
Reputation: 49
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)
Upvotes: 4
Reputation: 173
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
Upvotes: 0
Reputation: 1483
I figured out the problem: I was not adding class Team to the Hibernate AnnotationConfiguration
object. Thus, Hibernate was not recognizing the class.
Upvotes: 84
Reputation: 81
Another solution: Check to ensure that the referenced class is included your hibernate.cfg.xml
file.
Upvotes: 8
Reputation: 48
Add the class in hibernate.cfg
in proper order. First map the file that is going to be referred by another class
Upvotes: 0
Reputation: 1254
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.
Upvotes: 4
Reputation: 1
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
Upvotes: 0
Reputation: 1455
Along with entry in hibernate.cfg.xml, you'll need @Entity
annotation on referenced class.
Upvotes: 25