我使用的是注解的方法
package com.security.model.menu;
// Generated 2014-4-4 16:51:48 by Hibernate Tools 3.4.0.CR1
import static javax.persistence.GenerationType.IDENTITY;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.OrderBy;
/**
* Menu generated by hbm2java
*/
@Entity
@Table(name = "menu", catalog = "security")
public class Menu implements java.io.Serializable {
private Integer id;
private Menu menu;
private String name;
private String url;
private Integer orderNum;
private List<Menu> menus = new ArrayList<Menu>();
public Menu() {
}
public Menu(Menu menu, String name, String url, List<Menu> menus) {
this.menu = menu;
this.name = name;
this.url = url;
this.menus = menus;
}
public Integer getOrderNum() {
return orderNum;
}
public void setOrderNum(Integer orderNum) {
this.orderNum = orderNum;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parentId")
public Menu getMenu() {
return this.menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}
@Column(name = "name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "url")
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "menu")
@OrderBy(clause = "orderNum ASC")
public List<Menu> getMenus() {
return this.menus;
}
public void setMenus(List<Menu> menus) {
this.menus = menus;
}
}
注意:
1、要把hibernate生成的原有的Set改成List
2、
@OrderBy(clause = "orderNum ASC")