如何从单个数据库表

时间:2017-06-09 06:52:31

标签: java json spring hibernate spring-rest

我有数据库表

+-------------+----------------------+--------+
| category_id | name                 |parent_id  |
+-------------+----------------------+--------+
|           1 | ELECTRONICS          |   NULL |
|           2 | TELEVISIONS          |      1 |
|           3 | TUBE                 |      2 |
|           4 | LCD                  |      2 |
|           5 | PLASMA               |      2 |
|           6 | PORTABLE ELECTRONICS |      1 |
|           7 | MP3 PLAYERS          |      6 |
|           8 | FLASH                |      7 |
|           9 | CD PLAYERS           |      6 |
|          10 | 2 WAY RADIOS         |      6 |
+-------------+----------------------+--------+

我用这种方式设计了hibernate实体但是我无法得到以下json格式中提到的预期输出:

@Entity
@Table(name = "category")
public class Category {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "category_id")
  private short categoryId;
  @Column(name = "name")
  private String name;

  @Column(name = "parent_id")
  private Integer parentId;

  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinColumn(name = "parent_id")
  private Set<Category> child;

  public short getCategoryId() {
        return categoryId;
  }

  public void setCategoryId(short categoryId) {
        this.categoryId = categoryId;
  }

  public String getName() {
        return name;
  }

  public void setName(String name) {
        this.name = name;
  }

  public Integer getParentId() {
        return parentId;
  }

  public void setParentId(Integer parentId) {
        this.parentId = parentId;
  }

  public Set<Category> getChild() {
        return child;
  }

  public void setChild(Set<Category> child) {
        this.child = child;
  }
  }

现在我希望以下面的json格式回复

[
{
"categoryId": 1,
"name": "ELECTRONICS",
"child": [
  {
    "categoryId": 2,
    "name": "TELEVISIONS",
    "child": [
      {
        "categoryId": 3,
        "name": "TUBE",
        "child": []
      },
      {
        "categoryId": 4,
        "name": "LCD",
        "child": []
      },
      {
        "categoryId": 5,
        "name": "PLASMA",
        "child": []
      }
    ]
  },
  {
    "categoryId": 6,
    "name": "PORTABLE ELECTRONICS",
    "child": [
      {
        "categoryId": 7,
        "name": "MP3 PLAYERS ",
        "child": [
          {
            "categoryId": 8,
            "name": "FLASH",
            "child": []
          }
        ]
      },
      {
        "categoryId": 9,
        "name": "CD PLAYERS",
        "child": []
      },
      {
        "categoryId": 10,
        "name": "2 WAY RADIOS",
        "child": []
      }
    ]
  }
]
}
]

当我尝试使用此代码列出类别列表并发送响应时,我将获得以下异常:

@RequestMapping(value = "/fetchCategory", method = RequestMethod.GET)
  public List<Category> fetchCategory() {
    List<Category> categories = null;
        try {
            categories = masterConfigurationService.fetchCategory();
        } catch (ServiceException e) {
              LOGGER.error(e.getMessage(), e);
        }
        return categories;
  }

DAO代码是

session.createCriteria(Category.class).list();

更新: - 更新了实体 - 从实体中删除了父映射

现在,如果与两个孩子有关系,我将获得两次父树。

0 个答案:

没有答案