hibernate做无限极菜单树如此简单
程序员文章站
2022-06-08 08:38:27
...
package cn.isvi.entity; import java.util.HashSet; import java.util.Set; public class Menu { private Integer id; private String menuName; private String parentId; private Menu parentMenu; private Set<Menu> children=new HashSet<Menu>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getMenuName() { return menuName; } public void setMenuName(String menuName) { this.menuName = menuName; } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } public void addMenu(Menu menu){ children.add(menu); menu.parentMenu=this; } public Set<Menu> getChildren() { return children; } public void setChildren(Set<Menu> children) { this.children = children; } public Menu getParentMenu() { return parentMenu; } public void setParentMenu(Menu parentMenu) { this.parentMenu = parentMenu; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.isvi.entity"> <class name="Menu" table="Menu"> <id name="id" column="id"> <generator class="increment"> </generator> </id> <property name="menuName"></property> <property name="parentId"></property> <set name="children" cascade="save-update" lazy="false" inverse="true"> <key column="nc_id"></key> <one-to-many class="cn.isvi.entity.Menu"/> </set> <many-to-one name="parentMenu" column="nc_id"></many-to-one> </class> </hibernate-mapping>
package cn.isvi.test; import cn.isvi.dao.TemplateDAO; import cn.isvi.dao.impl.TemplateDAOImpl; import cn.isvi.entity.Menu; public class MenuTest { /** * @param args */ public static void main(String[] args) { TemplateDAO tdao=new TemplateDAOImpl(); Menu menu=new Menu(); menu.setMenuName("1级"); Menu s1=new Menu(); s1.setMenuName("2级"); menu.addMenu(s1); Menu s2=new Menu(); s2.setMenuName("3级"); s1.addMenu(s2); try { tdao.saveObj(menu); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> <mx:RemoteObject id="menus" destination="menus" endpoint="http://localhost:8400/GDGWeb/messagebroker/amf" result="resultHandle(event)" fault="faultHandle(event)"> </mx:RemoteObject> <mx:Script> <![CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.collections.ArrayCollection; [Bindable] private var hibernateData:ArrayCollection; private function getData():void{ menus.getMenus();//注意,不能查询所有的Menu,只查询root第一个Menu就可以了 } private function resultHandle(event:ResultEvent):void{ this.hibernateData=event.result as ArrayCollection; trace("得到数据"); } private function faultHandle(event:FaultEvent):void{ trace(event.message); } ]]> </mx:Script> <mx:Tree dataProvider="{hibernateData}" labelField="menuName" width="100%" height="90%"> </mx:Tree> <mx:Button label="得到数据" click="getData()"> </mx:Button> </mx:WindowedApplication>
推荐阅读