开发者社区> 问答> 正文

问大家一个问题:jpa一对多双向自身关联关系怎么配制啊?比如分类

问大家一个问题:jpa一对多双向自身关联关系怎么配制啊?比如分类

bean是这样写的:

package com.cms.entity;

 

import java.io.Serializable;

import java.util.ArrayList;

import java.util.List;

 

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.Transient;

 

import com.cms.common.hibernate.HibernateTree;

 

@Entity

public class Channel implements HibernateTree<Integer>,Serializable{

 private static final long serialVersionUID = 1L;

 private Integer id;

 private String name;

 private Channel parent;

 private List<Channel> children = new ArrayList<Channel>();

 private Integer lft;

 private Integer rgt;

 

 @Id

 @GeneratedValue

 @Column(name="channel_id")

 public Integer getId() {

 return id;

 }

 @Column(name="lft")

 public Integer getLft() {

 return lft;

 }

 @Column(name="channel_name")

 public String getName() {

 return name;

 }

 @Column(name="rgt")

 public Integer getRgt() {

 return rgt;

 }

 @ManyToOne(targetEntity=Channel.class)

 @JoinColumn(name="channel_parentid")

 public Channel getParent() {

 return parent;

 }

 //@Transient

 @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent") 

 public List<Channel> getChildren() {

 return children;

 }

 @Transient

 //hibernateTree重写不参与持久化

 public String getLftName() {

 return DEF_LEFT_NAME;

 }

 @Transient

 //hibernateTree重写

 public Integer getParentId() {

 Channel parent = getParent();

 if (parent != null) {

 return parent.getId();

 } else {

 return null;

 }

 }

 @Transient

 //hibernateTree重写

 public String getParentName() {

 return "channel_parentid";

 }

 @Transient

 //hibernateTree重写

 public String getRgtName() {

 return DEF_RIGHT_NAME;

 }

 @Transient

 //hibernateTree重写

 public String getTreeCondition() {

 return null;

 }

 public void setChildren(List<Channel> children) {

 this.children = children;

 }

 public void setId(Integer id) {

 this.id = id;

 }

 public void setLft(Integer lft) {

 this.lft=lft;

 }

 public void setName(String name) {

 this.name = name;

 }

 public void setParent(Channel parent) {

 this.parent = parent;

 }

 public void setRgt(Integer rgt) {

 this.rgt=rgt;

 }

}

  <#if list??>
       <#list list as channel> 
  <tr>
    <td>${channel.id}</td>
    <td>${channel.name}</td>
    <td>${channel.parent.id}</td>
    <td>${channel.lft}</td>
    <td>${channel.rgt}</td>
  </tr>
     </#list>
 </#if>

我现在是这样写的,在模板循环那个list的时候提示channel.parent is undefined找不到

展开
收起
a123456678 2016-03-12 18:57:11 3900 0
1 条回答
写回答
取消 提交回答
  • 应该加下判断
    
      <#if list??>
    
         <#list list as channel> 
    
      <tr>
    
        <td>${channel.id}</td>
    
        <td>${channel.name}</td>
    
        <td>${(channel.parent.name)!"hasn't parent!"}</td>
    
        <td>${channel.lft}</td>
    
        <td>${channel.rgt}</td>
    
      </tr>
    
       </#list>
    
     </#if>
    2019-07-17 19:01:28
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
关系网络分析(I+) 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载