[英]Hibernate mapping exception
I am getting the below error on trying to save an Employee
object to database: 我在尝试将
Employee
对象保存到数据库时遇到以下错误:
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] ** 与休眠有关的 pom.xml中的依赖项 [我的是Spring-Hibernate项目] **
<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. 我曾经在映射XML文件中定义Hibernate映射。 With this example, I was trying with annotations.
在这个例子中,我尝试使用注释。
Employee.java [only the beginning portion of code] Employee.java [仅代码的开头部分]
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. 我也尝试将
@Enitiy
更改为@org.hibernate.annotations.Entity
,但是没有用。
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? 让我知道如何在这里指定Employee Entity bean?
<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">
也已更改:
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.
谢谢大家
In addition to annotations, you need to let Hibernate know about your entity classes. 除了注释,您还需要让Hibernate知道您的实体类。 I think there are two ways of doing this:
我认为有两种方法可以做到这一点:
1) Add the class to your hibernate.cfg.xml file: 1)将类添加到您的hibernate.cfg.xml文件中:
<hibernate-configuration>
<session-factory>
<mapping class="com.example.common.Employee" />
</session-factory>
</hibernate-configuration>
2) Configure Hibernate in code: 2)在代码中配置Hibernate:
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);
}
An hibernate entity must define a no argument constructor. 休眠实体必须定义一个无参数的构造函数。
Source: http://coding.tocea.com/java/java-frameworks/hibernate/hibernate-entity-does-not-define-no-argument-constructor/ 资料来源: http : //coding.tocea.com/java/java-frameworks/hibernate/hibernate-entity-does-not-define-no-argument-constructor/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.