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;
}
}