I am receiving the following Hibernate Exception:
@OneToOne or @ManyToOne on Matchup.awayTeam references an unknown entity: Team
The simplified
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
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.
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
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>
Along with entry in hibernate.cfg.xml, you'll need @Entity
annotation on referenced class.
In my case it was persistence.xml
which listed all entity classes except one...