Reputation: 11112
I am getting the below error on trying to save an Employee
object to database:
org.hibernate.MappingException: Unknown entity: com.example.common.Employee
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:597)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1339)
Dependencies in pom.xml related to hibernate [Mine is a Spring-Hibernate project]**
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.7.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
I used to define Hibernate mappings in mapping XML file. With this example, I was trying with annotations.
Employee.java [only the beginning portion of code]
I am including the import statements too
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
@Table(name = "employees")
public class Employee implements Serializable {
private static final long serialVersionUID = 5468763051360122059L;
@Id
@Column(name = "employee_guid", length = 36)
@NotNull
@Size(min = 36, max = 36)
private String id;
@Column(name = "is_active", nullable = false)
private boolean isActive;
@ManyToMany(mappedBy = "employees", fetch = FetchType.LAZY, cascade = {
CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH
})
I tried changing @Enitiy
to @org.hibernate.annotations.Entity
too, but of no use.
Please give me some hint on where I am wrong. Please let me know if you need more code.
Edit:
Let me know how to specify Employee Entity bean here?
<bean id="MyHibernateSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<!-- if there is a mapping file like Employee.hbm.xml i can refer here, but i manage thru annotations -->
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.connection.isolation">2</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
<prop key="hibernate.current_session_context_class">managed</prop>
<prop key="hibernate.transaction.flush_before_completion">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
</props>
</property>
</bean>
I am trying with this one
<property name="annotatedClasses">
<list>
<value>com.example.Employee</value>
</list>
</property>
Also changed : class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
Update:
This question is incomplete unless more code is shared. Hence closing this for now, with the below suggestion as answer, though it is not the exact answer I was looking for. Will reopen when I get a chance. Thanks all.
Upvotes: 1
Views: 1812
Reputation: 335
In addition to annotations, you need to let Hibernate know about your entity classes. I think there are two ways of doing this:
1) Add the class to your hibernate.cfg.xml file:
<hibernate-configuration>
<session-factory>
<mapping class="com.example.common.Employee" />
</session-factory>
</hibernate-configuration>
2) Configure Hibernate in code:
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
private static SessionFactory buildSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
configuration.addAnnotatedClass(Employee.class);
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
}
Upvotes: 3