首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用多对一和一对多关系序列化list<object>到json

使用多对一和一对多关系序列化list<object>到json
EN

Stack Overflow用户
提问于 2012-12-09 15:26:10
回答 1查看 1.7K关注 0票数 1

我有班级菜单,这是一个自我,自我与许多人和许多关系。

代码语言:javascript
复制
package models;

import java.util.*;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;
import static play.data.validation.Constraints.*;
import javax.validation.*;

import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonManagedReference;

import com.avaje.ebean.*;
import play.i18n.Messages;
@Entity
public class Menu extends Model {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long id;

@Required
@MinLength(4)
@MaxLength(30)
public String name;

public String url;

@Transient
public boolean hasChild() {
    return url.isEmpty();
}

public Integer idx;

@Temporal(TemporalType.TIMESTAMP)
@Formats.DateTime(pattern = "yyyy/MM/dd hh:mm:ss")
public Date created;

@Required
public boolean enabled;

@ManyToOne
        @JsonBackReference
public Menu parent;

@OneToMany
@JsonManagedReference("parent")
public List<Menu> children;

public static Model.Finder<Long, Menu> find = new Model.Finder<Long, Menu>(Long.class, Menu.class);

public static List<Menu> findTops() {
    return find.where().eq("parent_id", null).eq("enabled", true).orderBy("idx asc").findList();
}


public static List<Menu> findChildsByParent(Menu parent) {
    return findChildsByParent(parent, true);
}

public static List<Menu> findChildsByParent(Menu parent, boolean enabled) {
    return find.where().eq("parent_id", parent.id).eq("enabled", enabled).orderBy("idx asc").findList();
}

public static boolean hasChilds(Menu parent) {
    return hasChilds(parent, true);
}

public static boolean hasChilds(Menu parent, boolean enabled) {
    return find.where().eq("parent_id", parent.id).eq("enabled", enabled).findList().size() > 0;
}

public static Page<Menu> findPage(int page, int size) {
    return find.findPagingList(size).getPage(page - 1);
}


public Menu() {
}
}

控制器中的代码是:

代码语言:javascript
复制
@BodyParser.Of(BodyParser.Json.class)
public static Result menuJson() {
    if (menus == null) {
        menus = Menu.find.all();
    }

    JsonNode json = new ObjectMapper().valueToTree(menus);

    return ok(json);
}   

错误详细信息为:

代码语言:javascript
复制
[RuntimeException: java.lang.IllegalArgumentException: Query threw SQLException:Unknown column 't1.menu_id' in 'on clause' Bind values:[1] Query was: select t0.id c0 , t1.id c1, t1.name c2, t1.url c3, t1.idx c4, t1.created c5, t1.enabled c6, t1.parent_id c7 from menu t0 left outer join menu t1 on t1.menu_id = t0.id where t0.id = ? order by t0.id (through reference chain: com.avaje.ebean.common.BeanList[0]->models.Menu["children"])] 

有没有很好的解决方案,或者如何声明一个自定义序列化?对于树模型,我没有很好的类设计对象,这个环境没有更好的设计吗?

EN

回答 1

Stack Overflow用户

发布于 2012-12-09 15:45:10

我解决了这些问题。

在OneToMany上添加一个映射是可行的。

代码语言:javascript
复制
package models;

import java.util.*;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;
import static play.data.validation.Constraints.*;
import javax.validation.*;

import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonManagedReference;

import com.avaje.ebean.*;
import play.i18n.Messages;

@Entity
public class Menu extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    @Required
    @MinLength(4)
    @MaxLength(30)
    public String name;

    public String url;

    @Transient
    public boolean hasChild() {
        return url.isEmpty();
    }

    public Integer idx;

    @Temporal(TemporalType.TIMESTAMP)
    @Formats.DateTime(pattern = "yyyy/MM/dd hh:mm:ss")
    public Date created;

    @Required
    public boolean enabled;

    @ManyToOne
    @JsonBackReference
    public Menu parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.PERSIST)
    @JsonManagedReference
    public List<Menu> children;

    public static Model.Finder<Long, Menu> find = new Model.Finder<Long, Menu>(Long.class, Menu.class);

    public static List<Menu> findTops() {
        return find.where().eq("parent_id", null).eq("enabled", true).orderBy("idx asc").findList();
    }


    public static List<Menu> findChildsByParent(Menu parent) {
        return findChildsByParent(parent, true);
    }

    public static List<Menu> findChildsByParent(Menu parent, boolean enabled) {
        return find.where().eq("parent_id", parent.id).eq("enabled", enabled).orderBy("idx asc").findList();
    }

    public static boolean hasChilds(Menu parent) {
        return hasChilds(parent, true);
    }

    public static boolean hasChilds(Menu parent, boolean enabled) {
        return find.where().eq("parent_id", parent.id).eq("enabled", enabled).findList().size() > 0;
    }

    public static Page<Menu> findPage(int page, int size) {
        return find.findPagingList(size).getPage(page - 1);
    }


    public Menu() {
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13785530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档