SpringMVC + Hibernate:criteria.list()返回一个空列表

时间:2015-10-25 16:33:02

标签: spring hibernate spring-mvc

我正在使用带有Hibernate的spring MVC,目的是获取表数据并将其存储在list.Here中使用的实体类:

package com.bng.core.entity;

// default package
// Generated Oct 25, 2015 4:38:03 PM by Hibernate Tools 3.4.0.CR1

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * servicenames generated by hbm2java
 */
@Entity
@Table(name = "servicenames")
public class ServiceNames implements java.io.Serializable {

    private Integer id;
    private String serviceName;

    public ServiceNames() {
    }

    public ServiceNames(String servicename) {
        this.serviceName = servicename;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "servicename", length = 25)
    public String getServiceName() {
        return this.serviceName;
    }

    public void setServiceName(String servicename) {
        this.serviceName = servicename;
    }


}

用于获取列表的方法:

@Transactional
    @Override
    public List<ServiceNames> getServiceNames() {
        Logger.sysLog(LogValues.APP_INFO, this.getClass().getName(), "Getting all Service names.");
        Session session = sessionFactoryGlobal.openSession();
        Criteria criteria = session.createCriteria(ServiceNames.class);
        List<ServiceNames> serviceNamesList = criteria.list();
        session.close();
        return serviceNamesList;
    }

调用该方法时,它返回一个空列表。请说明哪里出错了?

1 个答案:

答案 0 :(得分:0)

我认为你确定你的表servicenames有数据。所以这个问题可能是@Transactional无法正常工作的时候。通过手动打开和关闭交易,尝试获取不含@Transactional的列表。