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捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
相关推荐
在IT领域,尤其是在Java开发中,Hibernate是一个非常流行的对象关系映射(ORM)框架,它允许开发者使用面向对象的方式操作数据库。尽管ORM的核心是将Java对象映射到数据库表,但有时我们还需要直接利用数据库提供的...
本篇文章将详细讲解如何配置Hibernate框架以使用Proxool连接池。 首先,我们需要了解Hibernate和Proxool的基本概念。Hibernate是一个流行的Java ORM(对象关系映射)框架,它简化了Java应用与数据库之间的交互,...
16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) 20.1. Summary 20.2. SchemaExport...
在Java开发中,使用Hibernate作为持久层框架时,为了提高数据库操作的性能和资源利用率,通常会引入连接池技术。Proxool是Apache的一个开源项目,它提供了一个轻量级的数据库连接池实现。本篇将详细介绍如何在...
16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) 20.1. Summary 20.2. SchemaExport...
16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) 20.1. Summary 20.2. SchemaExport...
Query query = session.createQuery("from ClassName as alias"); ``` - **条件查询**: ```java Query query = session.createQuery("from User as user where user.name=? and user.pw=?"); query.setString...
Hibernate Criteria 是一种在Java应用程序中使用Hibernate ORM框架执行SQL查询的方式。它提供了一种面向对象的方式来构建查询,使得代码更加可读和易于维护。Criteria API 提供了多种方法来构建复杂的查询,包括添加...
鉴于Hibernate官方不再支持DBCP并推荐使用Proxool或C3P0,了解Proxool在Hibernate中的配置显得尤为重要。 配置Proxool通常涉及创建一个名为proxool.xml的配置文件,这个文件通常放置在应用程序的src根目录下,与...
别名和属性引用(Alias and property references) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用...
在使用Hibernate进行数据库操作时,经常需要对查询结果进行排序处理。对于简单的查询场景,使用HQL(Hibernate Query Language)即可轻松实现排序功能;但对于复杂的多表关联查询场景,则需要借助Criteria API来灵活...
- 如果查询结果中包含多张表的数据,可以使用`setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)`将结果转换为Map,键为列名,值为对应列的值。 通过理解并掌握以上内容,开发者可以有效地利用Hibernate将...
这里,`pool_alias`定义了连接池的别名,`hibernate.proxool.xml`指定了Proxool配置文件的位置,`connection.provider_class`则指明使用Proxool作为连接提供者。 **(3)Proxool配置文件** 创建`ProxoolConf.xml`...
16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency Strategy Support) 20.1. Summary 20.2. SchemaExport...
在某些情况下,开发者可能需要使用原生SQL查询来执行特定的数据库操作,这时就可以利用Hibernate的SQLQuery功能。本文将详细讲解如何使用Hibernate的SQLQuery进行本地SQL查询。 首先,创建SQLQuery实例是通过...
hibernate.proxool.pool_alias pool1 ## Only need one of the following #hibernate.proxool.existing_pool true #hibernate.proxool.xml proxool.xml #hibernate.proxool.properties proxool.properties ####...
<return alias="user" class="com.test.hibernate.db.User"> {call getUserInfo()} </hibernate-mapping> ``` #### 三、调用存储过程 配置好Hibernate映射文件后,接下来是在Java代码中调用这些...
criteria.createCriteria("relatedEntity", "alias", JoinType.INNER_JOIN); ``` 7. **动态关联抓取** 使用`fetchMode()`方法控制关联对象的懒加载或立即加载,以优化性能: ```java criteria.setFetchMode(...
为了使用 C3P0 连接池,我们需要在 Hibernate 配置文件(hibernate.cfg.xml)中添加以下配置: ``` <property name="hibernate.c3p0.min_size">5 <property name="hibernate.c3p0.max_size">20 <property name="...