0

It took me hours to realize, what the problem is:

I have a Spring Rest service and a GET-Method which returns a user in JSON-Format. The data comes from my database over sessionFactory.

After debugging it turned out, that the Problem is related to my bidrectional onetomany-manytoone relationship.

So calling

        User user = (User) sessionFactory.getCurrentSession().load(User.class, userId);

returns a User-Object where user.getCity().getSupplier() runs into an com.sun.jdi.InvocationException. Therefore Jackson is obviously unable to serialize. But what causes this exception?

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

private static final long serialVersionUID = 1L;

@Id
private long id;

@OneToMany(mappedBy = "city", cascade=CascadeType.ALL)
private Set<User> user;

@OneToMany(mappedBy = "city", cascade=CascadeType.ALL)
private Set<Supplier> supplier;

User:

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

private static final long serialVersionUID = 1L;

public User() {
}

@Id
private long id;

@ManyToOne
private City city;

Supplier:

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

private static final long serialVersionUID = 1L;

@Id
private long id;

@ManyToOne
private City city;
Jannik Weichert
  • 1,623
  • 16
  • 28

1 Answers1

0

As mentioned in the other answer, I think you'll find that your issues are related to the x-to-x relationships. This can sometimes create circular reference issues when trying to jsonify the entity beans.

Sometimes you can avoid or get past this by using annotations, other times a wrapper class is needed. I often just write a wrapper class to handle my JSON transactionts

There are many many references to this type of issues spanning many languages. Here a few starting points for you to research.

Community
  • 1
  • 1
Russell Shingleton
  • 3,176
  • 1
  • 21
  • 29