hibernate 自连接 注解 item parentItem ( 非级联删除 , 级联查询)
http://knight-black-bob.iteye.com/blog/2290675
本文 介绍 .setResultTransformer(Transformers.aliasToBean(MenuVo.class) 并非 内链接 ,注解内链接请看上面博客
package com.cailing.menu.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.codehaus.jackson.annotate.JsonAutoDetect; import org.slave4j.orm.hibernate.BaseEntity; @SuppressWarnings("serial") @JsonAutoDetect @Entity @Table(name = "t_activity") @org.hibernate.annotations.Proxy(lazy = false) public class Menu extends BaseEntity implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private Integer id; @Column(name = "short_name") private String short_name; @Column(name = "menu_name") private String menu_name; @Column(name = "url") private String url; @Column(name = "state") private Integer state; @Column(name = "seq") private Integer seq; @Column(name = "level") private Integer level; @Column(name = "children") private Integer children; @Column(name = "parent_id") private Integer parent_id; @Column(name = "image") private String image; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getShort_name() { return short_name; } public void setShort_name(String short_name) { this.short_name = short_name; } public String getMenu_name() { return menu_name; } public void setMenu_name(String menu_name) { this.menu_name = menu_name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } public Integer getSeq() { return seq; } public void setSeq(Integer seq) { this.seq = seq; } public Integer getLevel() { return level; } public void setLevel(Integer level) { this.level = level; } public Integer getChildren() { return children; } public void setChildren(Integer children) { this.children = children; } public Integer getParent_id() { return parent_id; } public void setParent_id(Integer parent_id) { this.parent_id = parent_id; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } }
package com.cailing.menu.entity; import java.util.List; public class MenuVo { // M.id, M.parent_id,M.`LEVEL`,M.menu_name,M.children,M.URL,M.SEQ,M.IMAGE private Integer id; private Integer parentId; private Integer level; private String menuName; private Integer children; private String url; private Integer seq; private String image; private List<MenuVo> childrenList; @Override public String toString() { return "MenuVo [id=" + id + ", parentId=" + parentId + ", level=" + level + ", menuName=" + menuName + ", children=" + children + ", url=" + url + ", seq=" + seq + ", image=" + image + ", childrenList=" + (childrenList != null ? childrenList.toString() : null) + "]"; } public List<MenuVo> getChildrenList() { return childrenList; } public void setChildrenList(List<MenuVo> childrenList) { this.childrenList = childrenList; } public Integer getSeq() { return seq; } public void setSeq(Integer seq) { this.seq = seq; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } public Integer getLevel() { return level; } public void setLevel(Integer level) { this.level = level; } public String getMenuName() { return menuName; } public void setMenuName(String menuName) { this.menuName = menuName; } public Integer getChildren() { return children; } public void setChildren(Integer children) { this.children = children; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } }
package com.cailing.menu.dao; import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Query; import org.hibernate.transform.Transformers; import org.springframework.stereotype.Repository; import com.cailing.base.BaseDao; import com.cailing.menu.entity.Menu; import com.cailing.menu.entity.MenuVo; @Repository public class MenuDao extends BaseDao<Menu>{ public List<MenuVo> getList(Integer userId,Integer parentId){ String sql ="SELECT M.id id, M.parent_id parentId,M.LEVEL level ,M.menu_name menuName,M.children children,M.URL url,M.SEQ seq,M.IMAGE image FROM T_MENU M "+ " INNER JOIN T_ROLE_MENU R ON M.id = R.menu_id INNER JOIN T_ROLE E ON R.role_id = E.ID "+ " INNER JOIN T_USER_ROLE L ON E.ID = L.role_id INNER JOIN T_USER S ON L.user_id = S.ID "+ " WHERE S.ID=? AND M.parent_id=? ORDER BY M.SEQ ASC "; /* private Integer id; private Integer parentId; private Integer level; private String menuName; private Integer children; private String url; private Integer seq; private String image;*/ Query query = this.getSession().createSQLQuery(sql) .addScalar("id", Hibernate.INTEGER) .addScalar("parentId", Hibernate.INTEGER) .addScalar("level", Hibernate.INTEGER) .addScalar("menuName", Hibernate.STRING) .addScalar("children", Hibernate.INTEGER) .addScalar("url", Hibernate.STRING) .addScalar("seq", Hibernate.INTEGER) .addScalar("image", Hibernate.STRING) .setResultTransformer(Transformers.aliasToBean(MenuVo.class)); query.setInteger(0, userId); query.setInteger(1, parentId); return query.list(); } public List<MenuVo> getList(Integer userId){ List<MenuVo> parentList = this.getList(userId, -1); List<MenuVo> childrenList = null; for (MenuVo menuVo : parentList) { childrenList = this.getList(userId, menuVo.getId()); menuVo.setChildrenList(childrenList); } return parentList; } }
package com.cailing.activity.service; import java.util.List; import javax.annotation.Resource; import org.slave4j.orm.hibernate.BaseDao; import org.slave4j.orm.hibernate.BaseService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.cailing.activity.dao.ActivityDao; import com.cailing.activity.entity.Activity; @Service @Transactional public class ActivityService extends BaseService<Activity>{ @Override @Resource(name = "activityDao") public void setBaseDao(BaseDao<Activity> baseDao) { this.baseDao = baseDao; } public List<Activity> getIndexActivity() { return ((ActivityDao)this.baseDao).getIndexActivity(); } public List<Activity> getTest() { return ((ActivityDao)this.baseDao).getTest(); } }
package com.cailing.menu.service; import java.util.List; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cailing.menu.entity.MenuVo; public class MenuServiceTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath*:applicationContext*.xml" }); MenuService service = (MenuService) context.getBean("menuService"); List<MenuVo> list = service.getList(2); System.out.println(list.toString()); } }