Hibernate映射异常

时间:2014-04-08 22:44:55

标签: java spring hibernate

我在尝试将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)

与hibernate相关的 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>

我曾经在映射XML文件中定义了Hibernate映射。在这个例子中,我正在尝试注释。

Employee.java [仅代码的开头部分]

我也包括了导入语句

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
    })

我也尝试将@Enitiy更改为@org.hibernate.annotations.Entity,但没有用。

请给我一些关于我错的地方的提示。如果您需要更多代码,请与我们联系。

修改:

让我知道如何在这里指定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>

我正在尝试这个

<property name="annotatedClasses">
            <list>
                <value>com.example.Employee</value>
            </list>
        </property>

也改变了:class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

更新

除非共享更多代码,否则此问题不完整。因此现在关闭这个,以下建议作为答案,虽然这不是我正在寻找的确切答案。当我有机会时会重新打开。谢谢大家。

2 个答案:

答案 0 :(得分:3)

除了注释之外,您还需要让Hibernate了解您的实体类。我认为有两种方法可以做到这一点:

1)将类添加到hibernate.cfg.xml文件中:

<hibernate-configuration>
  <session-factory>
    <mapping class="com.example.common.Employee" />
  </session-factory>
</hibernate-configuration>

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);
}

答案 1 :(得分:1)