Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.green.modules.sys.entity; import java.util.List; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table; import javax.persistence.Transient; import javax.validation.constraints.NotNull; import org.apache.commons.lang.ObjectUtils; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; import org.hibernate.annotations.Where; import org.hibernate.validator.constraints.Length; import com.google.common.collect.Lists; import com.green.common.persistence.IdEntity; /** * ??Entity * @author ThinkGem * @version 2013-05-15 */ @Entity @Table(name = "sys_menu") @DynamicInsert @DynamicUpdate @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Menu extends IdEntity<Menu> { private static final long serialVersionUID = 1L; private Menu parent; // ?? private String parentIds; // ? private String name; // ?? private String href; // private String target; // mainFrame?_blank?_self?_parent?_top private String icon; // private Integer sort; // ? private String isShow; // ???10? private String isActiviti; // ???1?0?? private String permission; // ?? private List<Menu> childList = Lists.newArrayList();// ??? private List<Role> roleList = Lists.newArrayList(); // public Menu() { super(); this.sort = 30; } public Menu(String id) { this(); this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parent_id") @NotFound(action = NotFoundAction.IGNORE) @NotNull public Menu getParent() { return parent; } public void setParent(Menu parent) { this.parent = parent; } @Length(min = 1, max = 255) public String getParentIds() { return parentIds; } public void setParentIds(String parentIds) { this.parentIds = parentIds; } @Length(min = 1, max = 100) public String getName() { return name; } public void setName(String name) { this.name = name; } @Length(min = 0, max = 255) public String getHref() { return href; } public void setHref(String href) { this.href = href; } @Length(min = 0, max = 20) public String getTarget() { return target; } public void setTarget(String target) { this.target = target; } @Length(min = 0, max = 100) public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } @NotNull public Integer getSort() { return sort; } public void setSort(Integer sort) { this.sort = sort; } @Length(min = 1, max = 1) public String getIsShow() { return isShow; } public void setIsShow(String isShow) { this.isShow = isShow; } @Length(min = 1, max = 1) public String getIsActiviti() { return isActiviti; } public void setIsActiviti(String isActiviti) { this.isActiviti = isActiviti; } @Length(min = 0, max = 200) public String getPermission() { return permission; } public void setPermission(String permission) { this.permission = permission; } @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'") @OrderBy(value = "sort") @Fetch(FetchMode.SUBSELECT) @NotFound(action = NotFoundAction.IGNORE) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public List<Menu> getChildList() { return childList; } public void setChildList(List<Menu> childList) { this.childList = childList; } @ManyToMany(mappedBy = "menuList", fetch = FetchType.LAZY) @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'") @OrderBy("id") @Fetch(FetchMode.SUBSELECT) @NotFound(action = NotFoundAction.IGNORE) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public List<Role> getRoleList() { return roleList; } public void setRoleList(List<Role> roleList) { this.roleList = roleList; } @Transient public static void sortList(List<Menu> list, List<Menu> sourcelist, String parentId) { for (int i = 0; i < sourcelist.size(); i++) { Menu e = sourcelist.get(i); if (e.getParent() != null && e.getParent().getId() != null && e.getParent().getId().equals(parentId)) { list.add(e); // ??, ?? for (int j = 0; j < sourcelist.size(); j++) { Menu child = sourcelist.get(j); if (child.getParent() != null && child.getParent().getId() != null && child.getParent().getId().equals(e.getId())) { sortList(list, sourcelist, e.getId()); break; } } } } } @Transient public boolean isRoot() { return isRoot(this.id); } @Transient public static boolean isRoot(String id) { return id != null && id.equals("1"); } @Transient public String getActivitiGroupId() { return ObjectUtils.toString(getPermission()); } @Transient public String getActivitiGroupName() { return ObjectUtils.toString(getId()); } }