使用spring hibernate模板

时间:2015-12-26 12:55:50

标签: java spring hibernate spring-mvc

我想点击一个按钮并从jsp page.DAO类方法getSongs()中检索歌曲,但它不起作用。我无法使用spring hibernate模板从数据库中检索列表。

Songs.java

package mytunes.pojo;

public class Songs {
    private int songId;
    private String title;
    private String duration;
    private String Artist;
    private String Director;
    private String language;
    private String genre;
    private String Cover;
    private int year;
    private String Company;
    private String Sample;
    private String Specification;
    private String Awards;

    //getters and setters
}

service-retrievesongs.java

public class RetriveSongs {

    public List<Songs> retriveSongs(HttpServletRequest request) {
        BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

        SongsDao dao=(SongsDao)factory.getBean("s");
        System.out.println("service ma");
        List<Songs> songslist = dao.getSongs("vande mataram");

        if(songslist != null)
        {
            return songslist;
        }
        return null;
    } 
}

DAO-SongsDao.java

package mytunes.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import mytunes.*;
import mytunes.pojo.Customer;
import mytunes.pojo.Songs;
import mytunes.util.HibernateUtil;

public class SongsDao {

    HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

    int id=0;

    public int saveSong(Songs s) {
        System.out.println(s.getTitle());
        List<?> list = new ArrayList<Songs>();
        DetachedCriteria criteria = DetachedCriteria.forClass(Songs.class)
                    .add(Restrictions.eq("title", s.getTitle()));
        list=template.findByCriteria(criteria);
        if (list.size() == 0) {
            template.update(s);
            id = s.getSongId();
            System.out.println("upadet");
        } else{
            //else add new CD in the store...
            id = (Integer)template.save(s);
            System.out.println("save");
        }
        return id;
    }

    public List<Songs> getSongs(String title){  

        List<Songs> list = new ArrayList<Songs>();
        list=template.find("from Songs");
        System.out.println(((Songs) list.get(0)).getArtist());

        return list;
    }
}

我收到此错误消息:

Hibernate: select songs0_.SONG_ID as SONG1_1_, songs0_.TITLE as TITLE1_,     songs0_.DURATION as DURATION1_, songs0_.LANGUAGE as LANGUAGE1_, songs0_.YEAR_RECORDED as YEAR5_1_, songs0_.DIRECTOR as DIRECTOR1_, songs0_.ARTIST as ARTIST1_, songs0_.GENRE as GENRE1_, songs0_.COVER_PATH as COVER9_1_, songs0_.COMPANY as COMPANY1_, songs0_.AWARDS as AWARDS1_, songs0_.SPECIFICATION as SPECIFI12_1_, songs0_.SAMPLE_PATH as SAMPLE13_1_ from SONGS songs0_

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at mytunes.dao.SongsDao.getSongs(SongsDao.java:57)
at mytunes.service.RetriveSongs.retriveSongs(RetriveSongs.java:29)
at mytunes.controller.ControllerServlet.doPost(ControllerServlet.java:283)
at mytunes.controller.ControllerServlet.doGet(ControllerServlet.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

0 个答案:

没有答案