i have 2 tables,combo_comment
and combo
(it would be something along the lines of a forum post or a question in stack overflow) where the combo_id
is a foreign key in combo_comment
, i want to select a comment based on a combo_id
without using any type of join, i want to basically do this
SELECT * FROM COMBO_COMMENT where COMBO_ID= 3;
where i'm referring to COMBO_COMMENT.COMBO_ID
, not COMBO.COMBO_ID
, however when i try to do this with hibernate it returns the combo object inside the comment, i know i can use @JsonIgnore
to ignore the combo object when returning the answer but then i wont have the combo_id field. i have tried using criteria and HQL
here's my combo_comment
entity class without the getter and setter(they don't have any type of annotation)
@Entity
@Table(name="COMBO_COMMENT")
public class ComboComment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="COMMENT_ID",nullable = false)
private int commentId;
@ManyToOne(fetch = FetchType.EAGER,targetEntity = Combo.class,cascade = CascadeType.ALL)
@JoinColumn(name = "COMBO_ID",nullable = false,referencedColumnName="COMBO_ID")
private Combo combo;
@Column(name="COMBO_COMMENT",nullable = false)
private String ComboComment;
@Column(name="POST_DATE",nullable = false)
private long postDate;
public ComboComment(){
}
}
and here is the two ways i'm trying to do it
with criteria:
Criteria criteria = createEntityCriteria();
criteria.add(Restrictions.eq("combo.comboId", comboId));
criteria.addOrder(Order.desc("postDate"));
criteria.setMaxResults(10);
if (page != 0) {
criteria.setFirstResult(page * 10);
}
with HQL:
String hql = "from ComboComment where combo.comboId =:comboId";
Query query = getSession().createQuery(hql).setInteger("comboId", comboId);
those are the only two ways i have manage to get a successful response without any type of exception
Update:
Exception when setting FetchType
to LAZY
HTTP Status 500 - Could not write content: could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.mycompany.ufggservice.model.ComboComment["combo"]->com.mycompany.ufggservice.model.Combo_$$_jvst3d5_2["comboId"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.mycompany.ufggservice.model.ComboComment["combo"]->com.mycompany.ufggservice.model.Combo_$$_jvst3d5_2["comboId"])