问题描述
我是 Hibernate 的新手.最近,我正在尝试使用 Spring 和 Hibernate 将我的 UI 与数据库连接的简单示例.
I am new to Hibernate. Recently, I was trying simple example to connect my UI with Database using Spring and Hibernate.
我能够成功调用一种方法来使用 REST 通过我的控制器、服务等获取数据.但是每当我运行应用程序时,我都会遇到以下错误.这里的Feedback"是数据库中表的名称以及与我的 Pojo Java 类相同的名称.
I am able to successfully call a method to fetch the data through my controller, service etc using REST.But I am encountering below error,whenever I run the application.Here "Feedback" is the name of Table in Database as well as the same name of my Pojo Java class.
注意:为表和 Java 类指定不同的名称也会导致相同的错误.
Note : Giving different names to table and Java class also results in same error.
org.springframework.orm.hibernate3.HibernateQueryException:反馈未映射 [来自反馈];嵌套异常是org.hibernate.hql.ast.QuerySyntaxException:反馈未映射[来自反馈]
Java Pojo:-
Java Pojo:-
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Feedback")
public class Feedback {
private int id;
private String title;
private String content;
private String name;
@Id
@GeneratedValue
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="title", nullable=false)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name="content", nullable=false)
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Column(name="name", nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Feedback [id=" + id + ", title=" + title + ", content="
+ content + ", name=" + name + "]";
}
}
反馈DAO :-
@Repository
public class FeedbackDAO implements IFeedbackDAO {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
hibernateTemplate = new HibernateTemplate(sessionFactory);
}
@SuppressWarnings("unchecked")
public List<Feedback> getFeedbackList() {
// This line causes that error.
return hibernateTemplate.find("from Feedback");
}
...
...
}
在db-config.xml
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
这可能是什么原因造成的?
What could be causing this ?
我在这里遗漏了什么吗?
Do am I missing something here ?
谢谢
推荐答案
您可能遗漏了 sessionFactory 定义中的一件事.
You might have missed a thing in sessionFactory definition.
<bean id="sessionFactory" ...>
...
<property name="annotatedClasses">
<list>
<value><java package name here>.Feedback</value>
</list>
</property>
...
</bean>
这篇关于Java - Hibernate 异常 - 无法映射反馈 [来自反馈]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!