hibernate alias 使用

简介:   hibernate 自连接 注解 item parentItem ( 非级联删除 , 级联查询) http://knight-black-bob.iteye.com/blog/2290675   本文 介绍 .

 

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());
		 
	}		
		
}

 

 



 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

目录
相关文章
|
Java 数据库连接 数据库
Hibernate 中出现表名(XXX) is not mapped 问题
Hibernate 中出现表名(XXX) is not mapped 问题,检查以下3个原因
677 0
Hibernate 中出现表名(XXX) is not mapped 问题
|
Java 数据库连接 mybatis
mybatis报错:The alias 'xxx' is already mapped to the value '*.*.xxx'
The alias 'xxx' is already mapped to the value '*.*.xxx'
17704 0
|
3月前
|
SQL Java 数据库连接
Mybatis系列之 Error parsing SQL Mapper Configuration. Could not find resource com/zyz/mybatis/mapper/
文章讲述了在使用Mybatis时遇到的资源文件找不到的问题,并提供了通过修改Maven配置来解决资源文件编译到target目录下的方法。
Mybatis系列之 Error parsing SQL Mapper Configuration. Could not find resource com/zyz/mybatis/mapper/
|
Java 数据库连接 mybatis
mybatis报错Type interface xxx.Dao is not known to the MapperRegistry
mybatis报错Type interface xxx.Dao is not known to the MapperRegistry
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(hibernate.cfg.xml)
Hibernate中使用Criteria查询及注解——(hibernate.cfg.xml)
|
SQL Java 数据库连接
JdbcTemplate 、NamedParameterJdbcTemplate、SimpleJdbcTemplate的区别
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010741376/article/details/46501657 ...
889 0
|
Java 数据库连接 mybatis
MyBatis - Could not resolve type alias ‘XXX‘(实体类全局别名)
MyBatis - Could not resolve type alias ‘XXX‘(实体类全局别名)
264 0
|
Java 数据库连接 mybatis
MyBatis - <typeAliases> & @Alias(别名)
MyBatis - <typeAliases> & @Alias(别名)
155 0
|
XML Oracle Java
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)上
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)
235 0
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)上
|
SQL XML 安全
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)下
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)
160 0
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)下