1.
1 package edu.jlu.fuliang.dao.impl; 2 3 import java.util.List; 4 5 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 6 7 import edu.jlu.fuliang.dao.SingerDAO; 8 import edu.jlu.fuliang.domain.Band; 9 import edu.jlu.fuliang.domain.CompositeSinger;10 import edu.jlu.fuliang.domain.Singer;11 import edu.jlu.fuliang.domain.SingleSinger;12 13 public class SingerDAOImpl extends HibernateDaoSupport implements SingerDAO{14 15 public void deleteSinger(Long id) {16 getHibernateTemplate().delete(id);17 }18 19 public ListgetAllBands() {20 return getHibernateTemplate().find("from Band");21 }22 23 public List getAllCompositeSingers() {24 return getHibernateTemplate().find("from CompositeSinger");25 }26 27 public List getAllSingers() {28 return getHibernateTemplate().find("from Singer");29 }30 31 public List getAllSingleSingers() {32 return getHibernateTemplate().find("from SingleSinger");33 }34 35 public List getBandsByName(String name) {36 return getHibernateTemplate().find("from Band b where b.name=?", new String[]{name});37 }38 39 public Singer getSingerById(Long id) {40 return (Singer)getHibernateTemplate().get(Singer.class, id);41 }42 43 public List getSingerByRegion(String region) {44 return getHibernateTemplate().find("from Singer s where s.region=?", new String[]{region});45 }46 47 public List getSingersByName(String name) {48 return getHibernateTemplate().find("from Singer s where s.name=?",new String[]{name});49 }50 51 public List getSingleSingersByName(String name) {52 return getHibernateTemplate().find("from SingleSinger s where s.name=?",new String[]{name});53 }54 55 public void saveSinger(Singer singer) {56 getHibernateTemplate().save(singer);57 }58 59 public void updateSinger(Singer singer) {60 getHibernateTemplate().saveOrUpdate(singer);61 }62 }
2.
1 package edu.jlu.fuliang.dao.impl; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import org.hibernate.FetchMode; 7 import org.hibernate.HibernateException; 8 import org.hibernate.Query; 9 import org.hibernate.Session;10 import org.springframework.orm.hibernate3.HibernateCallback;11 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;12 13 import edu.jlu.fuliang.dao.SongDAO;14 import edu.jlu.fuliang.domain.Song;15 16 public class SongDAOImpl extends HibernateDaoSupport implements SongDAO{17 18 public void delete(long id) {19 Song song =(Song)getHibernateTemplate().get(Song.class,id);20 getHibernateTemplate().delete(song);21 }22 23 public ListfindAll() {24 return (List )getHibernateTemplate().execute(new HibernateCallback(){25 public Object doInHibernate(final Session session)26 throws HibernateException, SQLException {27 return session.createCriteria(Song.class).setFetchMode("", FetchMode.JOIN).list();28 }29 });30 }31 32 public Song findById(long id) {33 return (Song)getHibernateTemplate().get(Song.class, id);34 }35 36 public List findByName(String name) {37 return getHibernateTemplate().find("from Song s where s.name like '%" + name + "%'");38 }39 40 41 public void save(Song song) {42 getHibernateTemplate().saveOrUpdate(song);43 }44 45 46 public void update(Song song) {47 getHibernateTemplate().saveOrUpdate(song);48 }49 50 51 public List findBySingerRegion(final String region) {52 return (List )getHibernateTemplate().execute(new HibernateCallback(){53 54 public Object doInHibernate(Session session)55 throws HibernateException, SQLException {56 Query query = session.createQuery("from Song s left join fetch s.singer where s.singer.region=?");57 return query.setString(0, region).list();58 }59 });60 }61 }