org.hibernate.hql.internal.ast.QuerySyntaxException:数据未映射[来自Data]

时间:2017-04-20 17:42:15

标签: java spring hibernate

我尝试在序列化后从数据库中检索xml,这是来自控制器片段的端点

@RequestMapping(value = "/getAll", method = RequestMethod.GET, headers = "Accept=application/xml")
    @ResponseBody
    public List<Data> getAll() {

        List<Data> listOfCountries = oService.getAll();
        return listOfCountries;
    }

这是来自DAO类的代码片段

public List<Data> getAll() {
        Session session = this.sessionFactory.getCurrentSession();
        List<Data> countryList = session.createQuery("from Data").list();
        return countryList;
    }

这是看似不会映射的模型类

@XmlRootElement(name="Data")
@Entity
@Table(name="Data")
public class Data{

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    @Column(name="phone")
    private
    String phone;   

    @Column(name="counter")
    private
    String counter; 

    @Column(name="STREET_NAME")
    private
    String STREET_NAME; 

    @Column(name="address")

我在spring config xml文件中添加了这个注释映射,如图所示

<annotation-driven />
    <resources mapping="/resources/**" location="/resources/" />
<beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>org.model.Data</beans:value>
            </beans:list>
        </beans:property>

访问端点我收到此错误

org.hibernate.hql.internal.ast.QuerySyntaxException: Data is not mapped [from Data]
    org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)

调试我的代码的每一层后,我发现没有可能是修复的循环漏洞。请问有什么不对?

1 个答案:

答案 0 :(得分:0)

你应该按List<Data>投射结果: -

List<Data> result = (List<Data>) session.createQuery("from Data").list();

或者您也可以使用entityManager进行创建查询。

Query query = entityManager. createQuery("Select data from Data data");