2

I have two hibernate annotated classes. I am doing a hibernate query on the FirstTable. After retrieving the data from the list the object is converted to JSON string using GSON. The converted JSON string will contain the data for the child table also. My requirement is to read the first level data. It shouldn't query the childtable.

Steps Tried

To achieve this after fetching the object i have detached the object from entitymanager (also tried session.evict). Closed the hibernate session and entitymanager object. Tried to convert the object to JSON. While doing this i am getting LazyInitializationException. Is there a workaround or is there another way to achieve this.

@Entity(name = "FirstTable")
@Table(name = "FirstTable")
@AccessType("property")
public class FirstTable {
    private SecondTable _SecondTable;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "SecondTable_ID")
    public SecondTable getSecondTable() {
        return _SecondTable;
    }
    public FirstTable setSecondTable(SecondTable value) {
        this._SecondTable = value;
        return this;
    }
    private long id;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="Id")
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id=id;
    }
}

@Entity(name = "SecondTable")
@Table(name = "SecondTable")
@AccessType("property")
public class SecondTable {
    private String _RandomCol;
    @Column(name="RandomCol")
    public java.lang.String getRandomCol() {
        return _RandomCol;
    }
    public SecondTable setRandomCol(java.lang.String value) {
        this._RandomCol = value;
        return this;
    }
    private long id;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="OId")
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id=id;
    }
}

4 Answers4

0

private transient SecondTable _SecondTable;

Hammad
  • 91
  • 1
  • 10
0

I'm dealing with same issue now. As far as I understand this reply JPA Hibernate collections not lazily loaded if you don't want to load childs level you shouldn't call getChildren method. But it seems that you actually calling this method while trying to convert object to json.

In your case, if it's possible try not converting Object to Json automatically, but fill json manually putting there properties that you actually need. If it's not possible in your case look here JPA + Hibernate - How to get FK of child entity without fetching that entity?

There might be a better solution but I didn't found it.

Just found the better way here hibernate entity to json

Community
  • 1
  • 1
Evgen
  • 998
  • 2
  • 9
  • 21
0

The LazyInitializationException is happening because you are trying to retrieve the child objects out of transaction. Even though you have detacted the FirstTable entity, it is still an entity and the SecondTable entity is also still an entity and is marked as uninitialized. In other words, the FirstTable entity is not holding a SecondTable class, but a proxy to the SecondTable entity, which is what is throwing the LazyInitializationException.

You can fix it, as mentioned, by setting the _SecondTable property to null first. Then the json serializer will ignore the property.

It's more typically to create a DTO, or Data Transfer Object. This is done by making classes that are essentially clones of the Entities, but have only the properties that you want to transfer via json.

Assuming you plan to get around to working with RESTful services, probably supporting client-side view framework, e.g., AngularJs, having worked out DTO's will save you a lot of headaches.

Community
  • 1
  • 1
K.Nicholas
  • 8,797
  • 4
  • 34
  • 53
0

Agreed with Nicholas.

So One complementary thing, In my last work, I used the JACKSON provider with CXF. And even with the data child set to null, the provider serialized the property with "null" (example: {data = null}). Don't know if it was my fault or the couple JACKSON-CXF. Use an annotation like @JsonIgnore could be a solution.

But, it could be not efficient to put this type of annotation on your entity if you want a day to transfert you entity in totality. Perhaps one day, it would be necessary. SO thinks to do that or not.

So, in my case, i decide to

1) Let the entity without "JSON ANNOTATION" and transfert like it is if need

2) Create an intelligent DTO / POJO to transfer the data (no one DTO/POJO for each need)

Cris59195
  • 71
  • 7