序列化列表< object>与多音&与JSON相关的onetomany [英] serialize list<object> with manytoone & onetomany relational to json

查看:100
本文介绍了序列化列表< object>与多音&与JSON相关的onetomany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Menu类,它是一个具有多音和一对多关系的自我.

I have class Menu, it's a self to self with manytoone and onetomany relational.

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() {
}
}

在控制器代码中是:

@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);
}   

错误详细信息是:

[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"])] 

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

It's there have good solution to solve them or how to declare a custom serialize? For the tree model i don't have good object to class design,is't there a better design for this env.?

推荐答案

我解决了它们.

在OneToMany上添加映射是可行的.

Add a mapped on OneToMany is works.

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() {
    }
}

这篇关于序列化列表&lt; object&gt;与多音&amp;与JSON相关的onetomany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆