-1

straight to the point: I have a group that contains projects. I want that association to be handle with a foreign key, which is why it has a mappedby tag. My issue is that if I query for groups I get into an inifinite loop where the group lists the projects which contain the group which list the project which again contains the group.....and so on. My entities (minimal version):

@Entity
public class DBGroup {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;

   @OneToMany(mappedBy = "group",cascade=CascadeType.ALL,fetch = FetchType.EAGER)
   private List<Project> projects = new ArrayList<>();
}


@Entity
public class Project {
  @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;

@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn//added this because i read somewhere this would help somehow but it didnt
    private DBGroup group;
}

Can anyone help on how to avoid that loop? If I change the fetchtype to lazy in DBGroup I get a LazyInitializationEXception.

Any help is appreciated.

Barry
  • 337
  • 1
  • 2
  • 15
  • 1
    Another perfect example why you shouldn't use `FetchType.EAGER` – XtremeBaumer Jun 13 '19 at 10:45
  • So this setup would work if I used Lazy FetchType? Which would mean that I somehow would need to fix the LazyInitializationException – Barry Jun 13 '19 at 10:47
  • 1
    Gets into an infinite loop where? What are you doing with the entity once loaded? Serializing to JSON? – Alan Hay Jun 13 '19 at 10:49
  • 1
    `LazyInitializationException` is easily fixed by not accessing the properties outside of the transaction/session. So either you load depth X manually before leaving the transaction (by simply accessing the property) or you always open a new transaction/session to fetch the missing data – XtremeBaumer Jun 13 '19 at 10:49
  • But I am using the result to form a JAX-RS response. Which translates it to a json object and to do that I belive it has to iterate over all attributes, hence I have no control over that – Barry Jun 13 '19 at 10:53
  • These are your options: (1) Assuming all the data is loaded (or transaction is still open at point of serialization) and you actually want the relationship represented in the JSON then break the infinite recursion: https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue or (2) if the collection has not been loaded and you don't want it in the JSON then prevent access https://stackoverflow.com/questions/21708339/avoid-jackson-serialization-on-non-fetched-lazy-objects. – Alan Hay Jun 13 '19 at 11:05

1 Answers1

1

When the transaction ends you obtain an LazyInitializationEXception for all objects you didn't fetch.

If you get the object with a query add join fetch like:

select p from Project p join fetch p.group g

You can fetch a list via code calling the size method before exit the ejb.

Use FetchType.LAZY all time you can for prevent this especially if is a list.

Egeo
  • 140
  • 7
  • How can this be the accepted answer when it in no way answers the original question? – Alan Hay Jun 13 '19 at 11:09
  • Well the rest of you guys answered in comments, not allowing me to chose an other answer and I do not know if this also answers the question so I rewarded someone for trying to help! – Barry Jun 13 '19 at 11:20