五、hibernate中持久层操作(多表之间的关联关系操作,1对1,一对多,多对多、继承)
程序员文章站
2024-03-15 10:56:53
...
下面是全部都是双向关系的数据,所有数据的关系在https://blog.csdn.net/IT_CREATE/article/details/87370051有说
一、1对1关系:
(利用丈夫和妻子进行介绍,一个丈夫含有一个妻子,一个妻子含有一个丈夫)
HusbandBean类:
private Integer id;
private String husbandName;
private Integer age;
private WifeBean wife;
WifeBean类:
private Integer id;
private String wifeName;
private Integer age;
private HusbandBean hus;
持久层操作查询丈夫,得到分页数据:
1、利用hql语句进行查询
@Repository
public class HusbandDaoImpl implements IHusbandDao {
private Session session;
@Override
public void findHusbandBeanByWifeAge2PageBean(PageBean page, Integer age) {
// TODO Auto-generated method stub
String hql = "select count(*) From HusbandBean as h left join h.wife as w where w.age >= :age";
Query query = session.createQuery(hql);
query.setInteger("age", age);
long totalRows = (long) query.uniqueResult();
List<?> datas = null;
if(totalRows > 0) {
hql = "From HusbandBean as h left join fetch h.wife as w where w.age >= :age";
query = session.createQuery(hql);
query.setInteger("age", age);
query.setFirstResult(page.getIndex());
query.setMaxResults(page.getRows());
datas = query.list();
}
page.setTotalRows(totalRows);
page.setData(datas);
}
}
2、利用Criteria
@Repository
public class HusbandDaoImpl implements IHusbandDao {
private Session session;
@Override
public void findHusbandBeanByWifeAge2PageBean(PageBean page, Integer age) {
// TODO Auto-generated method stub
Criteria criteria = session.createCriteria(HusbandBean.class);
criteria.createAlias("wife", "w", JoinType.LEFT_OUTER_JOIN);
if(age != null) {
criteria.add(Restrictions.ge("w.age", age));
}
//加投影
criteria.setProjection(Projections.rowCount());
long totalRows = (long) criteria.uniqueResult();
List<?> datas = null;
if(totalRows > 0) {
//去掉投影
criteria.setProjection(null);
criteria.setFirstResult(page.getIndex());
criteria.setMaxResults(page.getRows());
criteria.setResultTransformer(Criteria.ROOT_ENTITY);//这句话的作用 === HQL中编写的fetch
datas = criteria.list();
}
page.setTotalRows(totalRows);
page.setData(datas);
}
}
二、一对多的关系
这里我用学生和老师进行举例,一个老师对应对个学生,一个学生对应一个老师
TeacherBean类:
private Integer id;
private String teacherName;
private Integer age;
private Set<StudentBean> stus;
StudentBean类:
private Integer id;
private String stuName;
private Integer age;
private TeacherBean teacher;
操作老师的查询方法(查询学生名字查出对应的老师):
1、利用hql
@Repository
public class TeacherDaoImpl implements ITeacherDao {
private Session session;
@Override
public List<?> findTeacherBeanByStudentName(String stuName) {
// TODO Auto-generated method stub
String hql = "select DISTINCT(t) From TeacherBean as t left join fetch t.stus as s where s.stuName like concat(:stuName,'%')";
Query query = session.createQuery(hql);
query.setString("stuName", stuName);
return query.list();
}
}
2、利用Criteria
@Repository
public class TeacherDaoImpl implements ITeacherDao {
private Session session;
@Override
public List<?> findTeacherBeanByStudentName(String stuName) {
// TODO Auto-generated method stub
Criteria criteria = session.createCriteria(TeacherBean.class);
criteria.createAlias("stus", "s",JoinType.LEFT_OUTER_JOIN);
if(!StringUtils.isEmpty(stuName)) {
criteria.add(Restrictions.like("s.stuName", stuName, MatchMode.START));
}
//去重复
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
}
操作学生:(通过查询老师名字,得到所有学生的分页数据)
1、利用hql
@Repository
public class StudentDaoImpl implements IStudentDao {
private Session session;
@Override
public void findStudentBeanByTeacherName2PageBean(PageBean page, String teacherName) {
// TODO Auto-generated method stub
String hql = "select count(*) From StudentBean as s left join s.teacher as t where t.teacherName like concat(:teacherName,'%')";
Query query = session.createQuery(hql);
query.setString("teacherName", teacherName);
long totalRows = (long) query.uniqueResult();
List<?> datas = null;
if(totalRows > 0) {
hql = "From StudentBean as s left join fetch s.teacher as t where t.teacherName like concat(:teacherName,'%')";
query = session.createQuery(hql);
query.setString("teacherName", teacherName);
query.setFirstResult(page.getIndex());
query.setMaxResults(page.getRows());
datas = query.list();
}
page.setTotalRows(totalRows);
page.setData(datas);
}
}
2、利用Criteria
@Repository
public class StudentDaoImpl implements IStudentDao {
private Session session;
@Override
public void findStudentBeanByTeacherName2PageBean(PageBean page, String teacherName) {
// TODO Auto-generated method stub
Criteria criteria = session.createCriteria(StudentBean.class);
criteria.createAlias("teacher", "t",JoinType.LEFT_OUTER_JOIN);
if(!StringUtils.isEmpty(teacherName)) {
criteria.add(Restrictions.like("t.teacherName", teacherName, MatchMode.START));
}
//添加投影
criteria.setProjection(Projections.rowCount());
long totalRows = (long) criteria.uniqueResult();
List<?> datas = null;
if(totalRows > 0) {
criteria.setProjection(null);
criteria.setFirstResult(page.getIndex());
criteria.setMaxResults(page.getRows());
criteria.setResultTransformer(Criteria.ROOT_ENTITY);//这句话的作用 === HQL中编写的fetch
datas = criteria.list();
}
page.setTotalRows(totalRows);
page.setData(datas);
}
}
三、多对多的关系
这里用玩家和游戏来举例,一个游戏可以多个玩家玩,一个玩家可以玩多个游戏
PlayerBean类:
private Integer id;
private String playerName;
private Set<GameBean> games;
GameBean类:
private Integer id;
private String gameName;
private Set<PlayerBean> players;
操作玩家数据(通过查询游戏名称,得到玩这个游戏的所有玩家)
1、利用hql
@Repository
public class PlayerDaoImpl implements IPlayerDao {
private Session session;
@Override
public List<?> findPlayerBeanByGameName(String gameName) {
// TODO Auto-generated method stub
String hql = "From PlayerBean as p left join fetch p.games as g where g.gameName like concat(:gameName,'%')";
Query query = session.createQuery(hql);
query.setString("gameName", gameName);
return query.list();
}
}
2、利用Criteria
@Repository
public class PlayerDaoImpl implements IPlayerDao {
private Session session;
@Override
public List<?> findPlayerBeanByGameName(String gameName) {
// TODO Auto-generated method stub
Criteria criteria = session.createCriteria(PlayerBean.class);
criteria.createAlias("games", "g");
if(!StringUtils.isEmpty(gameName)) {
criteria.add(Restrictions.like("g.gameName", gameName, MatchMode.START));
}
return criteria.list();
}
}
操作游戏数据(通过查询玩家名字得到该玩家所玩的所有游戏)
1、利用hql
@Repository
public class GameDaoImpl implements IGameDao {
private Session session;
@Override
public List<?> findGameBeanByPlayerName(String playerName) {
// TODO Auto-generated method stub
String hql = "select DISTINCT(g) From GameBean as g left join fetch g.players as p where p.playerName like concat(:playerName,'%')";
Query query = session.createQuery(hql);
query.setString("playerName", playerName);
return query.list();
}
}
2、利用Criteria
@Repository
public class GameDaoImpl implements IGameDao {
private Session session;
@Override
public List<?> findGameBeanByPlayerName(String playerName) {
// TODO Auto-generated method stub
Criteria criteria = session.createCriteria(GameBean.class);
criteria.createAlias("players", "p");
if(!StringUtils.isEmpty(playerName)) {
criteria.add(Restrictions.like("p.playerName", playerName,MatchMode.START));
}
//去重,并且将关联对象,装配到主对象身上去
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
}
四、继承的关系
这里我用宠物来介绍,宠物中狗和猫
PetBean类:
private Integer id;
private String petName;
DogBean类:(继承宠物类)
public class DogBean extends PetBean{
private Integer bone;
}
CatBean类:(继承宠物类)
public class CatBean extends PetBean{
private int fish;
}
通过名字查询出数据,是猫还是狗(得到结果集)
1、利用hql
@Repository
public class PetDaoImpl implements IPetDao {
private Session session;
@Override
public List<?> findPetBeanByName(String petName) {
// TODO Auto-generated method stub
String hql = "From PetBean as p where p.petName like concat(:petName,'%')";
Query query = session.createQuery(hql);
query.setString("petName", petName);
return query.list();
}
}
2、利用Criteria
@Repository
public class PetDaoImpl implements IPetDao {
private Session session;
@Override
public List<?> findPetBeanByName(String petName) {
// TODO Auto-generated method stub
Criteria criteria = session.createCriteria(PetBean.class);
if(!StringUtils.isEmpty(petName)) {
criteria.add(Restrictions.like("petName", petName,MatchMode.START));
}
return criteria.list();
}
}
上一篇: Vue组件通信