当我尝试从spring容器中操作person'实体'时,我收到以下错误:
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;
.....
.....
}
服务bean:
@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>
答案 0 :(得分:0)
添加到sessionFactory bean定义:
<property name="packagesToScan" value="common.**.entities" />
common.**.entities
- 包含您的实体。