1

当我尝试从 spring 容器中操作人员“实体”时出现以下错误:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.s
pring.entity.Person
        at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionF
actoryImpl.java:1141)
        at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.jav
a:1433)

我哪里出错了——也许是在尝试结合 xml 和带注释的元数据?
感谢您对此的任何帮助。

实体:

@Entity
@Table(name = "PERSON")
public class Person implements Serializable {

    private static final long serialVersionUID = -5527566248002296042L;

    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Integer id;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;
.....
.....
}

服务豆:

@Service("personService")
@Transactional
public class PersonService {


    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;

    public List<Person> getAll() {

        // Retrieve session from Hibernate
        Session session = sessionFactory.openSession();
        try{ 
        // Create a Hibernate query (HQL)
        Query query = session.createQuery("FROM  Person");

        // Retrieve all
        return  query.list();
        }
        finally{
        session.close();
        }
    }
....
....
}

主要的:

public static void main(String[] args){

        ApplicationContext appContext = 
                new ClassPathXmlApplicationContext("META-INF/beans-txn.xml");


        PersonService personService = (PersonService)appContext.getBean("personService");
        personService.add("Rob","Cahill", new Double(20000));
        List<Person> persons = personService.getAll();
    .....

弹簧配置:

<context:annotation-config />

    <bean id="personService" class="org.spring.service.PersonService"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        .....
    </bean>

        <!-- Hibernate session factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
        .....
        </property>

    </bean>
4

1 回答 1

1

添加到您的 sessionFactory bean 定义:

<property name="packagesToScan" value="common.**.entities" />

Where common.**.entities- 与您的实体打包。

于 2013-08-05T15:39:09.287 回答