Java 由于实体类注释配置,遇到hibernate映射异常
我收到此错误是因为我的应用程序无法为Java 由于实体类注释配置,遇到hibernate映射异常,java,spring,hibernate,Java,Spring,Hibernate,我收到此错误是因为我的应用程序无法为sessionFactory创建bean,也无法为transactionManager创建bean nested exception is org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.entity.Candidate"/> servlet.xml: 在使用时,您必须使用@Enti
sessionFactory
创建bean,也无法为transactionManager
创建bean
nested exception is org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.entity.Candidate"/>
servlet.xml:
在使用时,您必须使用
@Entity
和@Table
注释类
<mapping class="com.entity.Candidate"/>
请看一看我没有明白我违反了哪条规则,我的问题得分降到了-1?欢迎来到堆栈溢出!我尽我所能地编辑了你的问题。但是,添加代码和描述的解释,以便更多了解该主题的人看到它。请在遇到的特定错误消息中进行编辑,以防识别特定问题。祝你好运谢谢你的回复,但我已经把@Entity给了我的班级。但我还是得到了这个例外!!请参阅我的最新答案。请提供您的代码,以便为您提供更多帮助:)我在您的spring xml中没有看到类似
的内容。我忘了发布它,但我有这些代码行。我猜您使用hibernate 4,不用问了,对吗?
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/interview"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
@Autowired
SessionFactory sessionFactory;
@Override
public List<Candidate> getCandidatesList() {
// TODO Auto-generated method stub
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Candidate.class);
return criteria.list();
}
package com.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
@Entity
public class Candidate {
private int number;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@GenericGenerator(name = "system-uuid", strategy = "uuid")
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<context:component-scan base-package="com" >
<context:include-filter type="regex" expression="com.*"/>
</context:component-scan>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mapping class="com.entity.Candidate"/>
<mapping resource="com/entity/Candidate.hbm.xml" />
new AnnotationConfiguration().configure().buildSessionFactory();