1

I have a problem with transform data from List to Json(Gson). I have 2 entity with relation OneToMany and ManyToOne.I try use solution from answer in other post's, but didn't help.

Some info about my entity and error's.

Entity

 @Entity
    @Table(name = "Question")
    public class Question implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String question;

    @OneToMany(mappedBy="question")
    private List<Answer> answers;
    .....getters/setters
    @Override
    public String toString() {
        return "Question [id=" + id + ", question=" + question + ", answers="
                + answers + "]";
    }

Answer Entity

@Entity
public class Answer{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
//  @GeneratedValue
    private int id;

    private String answer;

    private byte isCorrect;

    @ManyToOne
    @JoinColumn(name="QuestionID")
    private Question question;
    .....getters/setters
    @Override
    public String toString() {
        return "Answer {id:" + id + ", answer:" + answer + ", isCorrect:"
                + isCorrect +  "}";
    }

DAO

   @Repository
@Transactional 
public class QuestionDAOImpl implements QuestionDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
//  @Transactional
    public List<Question> getQuestion() {
        // TODO Auto-generated method stub
        return sessionFactory.getCurrentSession().createQuery("from Question").list();
    }
}

Controller

@Controller
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Model model){
        List<Question> qu =questionService.getQuestion();
        Gson gson  = new Gson();
    String m = gson.toJson(qu);
    //Gson gs = new GsonBuilder().registerTypeAdapter(Question.class, myAdapter).create();
        System.out.println(qu);
        return "home";
    }
}

get error

java.lang.StackOverflowError
    com.google.gson.internal.$Gson$Types.equals($Gson$Types.java:158)
    com.google.gson.reflect.TypeToken.equals(TypeToken.java:284)
    java.util.HashMap.getEntry(Unknown Source)
    java.util.HashMap.get(Unknown Source)
    java.util.Collections$SynchronizedMap.get(Unknown Source)
    com.google.gson.Gson.getAdapter(Gson.java:332)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)

I get solution from answer

and now got new error

java.lang.IllegalArgumentException

com.google.gson.internal.$Gson$Preconditions.checkArgument($Gson$Preconditions.java:42)
        com.google.gson.GsonBuilder.registerTypeAdapter(GsonBuilder.java:448)
        ua.home.rusliakov.web.QuestionController.home(QuestionController.java:71)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        java.lang.reflect.Method.invoke(Unknown Source)

Maybe I to do something wrong.

Thx!

Community
  • 1
  • 1
user2483213
  • 321
  • 1
  • 3
  • 15

1 Answers1

0

You have a cycle in your object model. Question has a list of Answers and Answer knows its Question. That is not serializable to JSON because it would result in an JSON document with infinite length.

It will not work until you either model the relation between Question and Answer as a unidirectional one or you mark one side as "transient". But the second approach may have an impact on your JPA handling as well so I suggest to think about the first approach. Do you really need bidirectional relations or may it be sufficient if the Question knows its answers but not vice-versa?

roehrijn
  • 1,387
  • 1
  • 11
  • 20