比如要实现如下自关联效果
只需要注解 @OneToOne 即可,其他的不加
ex:
@Entity
@Table(name="menu")
public class Menu {
@Id
@GeneratedValue
private long id;
private String name;
private String url;
private String icon;
@OneToOne
private Menu parent;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Menu getParent() {
return parent;
}
public void setParent(Menu parent) {
this.parent = parent;
}
@Override
public String toString() {
return "Menu{" +
"id=" + id +
", name='" + name + '\'' +
", url='" + url + '\'' +
", icon='" + icon + '\'' +
", parent=" + parent +
'}';
}
}