2015-10-25 105 views
1

我在Hibernate中使用Spring MVC,其目的是獲取表數據並將其存儲在一個列表中。這裏使用的實體類:SpringMVC + Hibernate:criteria.list()返回一個空的列表

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; 
    } 

當調用該方法返回一個空列表。請提出它出錯的地方?

回答

0

我想你確定你的表servicenames有數據。所以這樣的問題可能在@Transactional無法正常工作。嘗試通過手動打開和關閉事務來獲取沒有@Transactional的列表。