当我尝试从弹簧容器中操纵人的“实体”时,出现以下错误:
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>
添加到您的sessionFactory bean定义中:
<property name="packagesToScan" value="common.**.entities" />
位置common.**.entities
-与您的实体包装在一起。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句