1.

 package edu.jlu.fuliang.dao.impl;

 import java.util.List;

 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 import edu.jlu.fuliang.dao.SingerDAO;
import edu.jlu.fuliang.domain.Band;
import edu.jlu.fuliang.domain.CompositeSinger;
import edu.jlu.fuliang.domain.Singer;
import edu.jlu.fuliang.domain.SingleSinger; public class SingerDAOImpl extends HibernateDaoSupport implements SingerDAO{ public void deleteSinger(Long id) {
getHibernateTemplate().delete(id);
} public List<Band> getAllBands() {
return getHibernateTemplate().find("from Band");
} public List<CompositeSinger> getAllCompositeSingers() {
return getHibernateTemplate().find("from CompositeSinger");
} public List<Singer> getAllSingers() {
return getHibernateTemplate().find("from Singer");
} public List<SingleSinger> getAllSingleSingers() {
return getHibernateTemplate().find("from SingleSinger");
} public List<Band> getBandsByName(String name) {
return getHibernateTemplate().find("from Band b where b.name=?", new String[]{name});
} public Singer getSingerById(Long id) {
return (Singer)getHibernateTemplate().get(Singer.class, id);
} public List<Singer> getSingerByRegion(String region) {
return getHibernateTemplate().find("from Singer s where s.region=?", new String[]{region});
} public List<Singer> getSingersByName(String name) {
return getHibernateTemplate().find("from Singer s where s.name=?",new String[]{name});
} public List<SingleSinger> getSingleSingersByName(String name) {
return getHibernateTemplate().find("from SingleSinger s where s.name=?",new String[]{name});
} public void saveSinger(Singer singer) {
getHibernateTemplate().save(singer);
} public void updateSinger(Singer singer) {
getHibernateTemplate().saveOrUpdate(singer);
}
}

2.

 package edu.jlu.fuliang.dao.impl;

 import java.sql.SQLException;
import java.util.List; import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import edu.jlu.fuliang.dao.SongDAO;
import edu.jlu.fuliang.domain.Song; public class SongDAOImpl extends HibernateDaoSupport implements SongDAO{ public void delete(long id) {
Song song =(Song)getHibernateTemplate().get(Song.class,id);
getHibernateTemplate().delete(song);
} public List<Song> findAll() {
return (List<Song>)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(final Session session)
throws HibernateException, SQLException {
return session.createCriteria(Song.class).setFetchMode("", FetchMode.JOIN).list();
}
});
} public Song findById(long id) {
return (Song)getHibernateTemplate().get(Song.class, id);
} public List<Song> findByName(String name) {
return getHibernateTemplate().find("from Song s where s.name like '%" + name + "%'");
} public void save(Song song) {
getHibernateTemplate().saveOrUpdate(song);
} public void update(Song song) {
getHibernateTemplate().saveOrUpdate(song);
} public List<Song> findBySingerRegion(final String region) {
return (List<Song>)getHibernateTemplate().execute(new HibernateCallback(){ public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery("from Song s left join fetch s.singer where s.singer.region=?");
return query.setString(0, region).list();
}
});
}
}
05-04 03:57