Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java json转换期间发生stackoverflow错误(hibernate双向映射)_Java_Hibernate_Gson_Bidirectional_Stack Overflow - Fatal编程技术网

Java json转换期间发生stackoverflow错误(hibernate双向映射)

Java json转换期间发生stackoverflow错误(hibernate双向映射),java,hibernate,gson,bidirectional,stack-overflow,Java,Hibernate,Gson,Bidirectional,Stack Overflow,我有两个实体类:Userclass和Questionclass,前者有一个问题列表,后者有一个User属性 用户类声明: @Entity @Table(schema="test",name="so_user") public class User { private List<Question> questions; //other attributes @OneToMany(cascade=CascadeType.ALL)

我有两个实体类:
User
class和
Question
class,前者有一个问题列表,后者有一个User属性

用户类声明:

 @Entity
    @Table(schema="test",name="so_user")
    public class User {
    private List<Question> questions;
    //other attributes
            @OneToMany(cascade=CascadeType.ALL)
        @PrimaryKeyJoinColumn   
        public List<Question> getQuestions() {
            return questions;
        }
    //.. other methods
    }   
@Entity
@Table(schema="test")
public class Question {
private User askedBy;
//other attributes
  @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
  @PrimaryKeyJoinColumn
  public User getAskedBy() {
    return askedBy;
  }

//.. other methods
}   
当我尝试使用
Gson库
将用户对象转换为Json时,会出现
stackoverflow错误
。原因:用户有一个问题列表,每个问题依次有 用户属性(在hibernate中用于双向映射)

测试代码:

public class MappingTest {
    public static void main(String[] args) {
        List<User> users = null;
        User user = null;
        //populate some users
        for (int i = 0; i < 10; i++) {
            if (i == 0)
                users = new ArrayList<User>();
            user = new User();
            List<Question> questions = null;
            Question question = null;
            //create dummy questions
            for (int questionCount = 0; questionCount < 5; questionCount++) {
                if(questionCount==0)
                    questions = new ArrayList<Question>();
                question = new Question();
                question.setAskedBy(user);
                System.out.println("question added is "+question);
                questions.add(question);
            }
            user.setQuestions(questions);
            users.add(user);
            System.out.println("user added is "+user);
        }
        //TODO
        System.out.println(users);
        Gson gson = new Gson();
        System.out.println(gson.toJson(users));

    }
}
将Jackson API放入(com.fasterxml.Jackson)应用程序中,并在realationship中使用此注释

@JSONback对反向类的引用(案例1-to-N中的列表或集合)

@JsonManagedReference到mappedBy类(在1到N的情况下为单个对象)

就你而言

@OneToMany(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
@JsonManagedReference   
public List<Question> getQuestions() {
     return questions;
}

______________________________________


@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@PrimaryKeyJoinColumn
@JsonBackReference
public User getAskedBy() {
    return askedBy;
}
@OneToMany(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
@JsonManagedReference
公共列表问题(){
回答问题;
}
______________________________________
@manytone(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@PrimaryKeyJoinColumn
@JsonBackReference
公共用户getAskedBy(){
歪歪扭扭地返回;
}
您需要使用Jackson(而不是gson)打印json

它在序列化中与双向递归一起工作。

将Jackson API放入(com.fasterxml.Jackson)应用程序中,并在realationship中使用此注释

@JSONback对反向类的引用(案例1-to-N中的列表或集合)

@JsonManagedReference到mappedBy类(在1到N的情况下为单个对象)

就你而言

@OneToMany(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
@JsonManagedReference   
public List<Question> getQuestions() {
     return questions;
}

______________________________________


@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@PrimaryKeyJoinColumn
@JsonBackReference
public User getAskedBy() {
    return askedBy;
}
@OneToMany(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
@JsonManagedReference
公共列表问题(){
回答问题;
}
______________________________________
@manytone(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@PrimaryKeyJoinColumn
@JsonBackReference
公共用户getAskedBy(){
歪歪扭扭地返回;
}
您需要使用Jackson(而不是gson)打印json

它在序列化中与双向递归一起工作。

看看这个:看看这个: