Today i met a weird hibernate exception, somehow it show that my User object does not exists. A little bit weird because i’m querying Comment tables, but the exceptions show “object user does not exist”.
A Weird Hibernate Error : org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.edw.entity.User#anonymousUser]
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.edw.entity.User#anonymousUser]
at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:145)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
at org.hibernate.type.EntityType.resolve(EntityType.java:379)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:120)
After some minutes digging on my sourcecode, i found out that javabean Comment has a @ManyToOne (Hibernate annotation for many to one relationship) to javabean User. Too bad one Comment object, still has a foreign key to a deleted User object.
This is my previous User class and Comment class, the main culprit is on line 30.
package com.edw.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name="tbl_comment")
public class Comment implements Serializable {
@Id
@GeneratedValue
private Long id;
@Temporal(TemporalType.TIMESTAMP)
private Date timestamp;
private String content;
@ManyToOne
private User user;
@ManyToOne
private News news;
public Long getId() {
return id;
}
// other setter and getter
}
package com.edw.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="tbl_user")
public class User implements Serializable {
@Id
private String username;
private String password;
private Boolean status;
private String email;
private String name;
private String address;
private String city;
private String job;
private String about;
private String relationship;
private String privacy;
private String role;
private String tokenRegistration;
private String tokenForgotPassword;
public User() {}
public User(String username) {
this.username = username;
}
}
The workaround is by adding @NotFound annotation to ignore the condition when Hibernate unable to find the User class.
package com.edw.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name="tbl_comment")
public class Comment implements Serializable {
@Id
@GeneratedValue
private Long id;
@Temporal(TemporalType.TIMESTAMP)
private Date timestamp;
private String content;
@NotFound(action=NotFoundAction.IGNORE)
@ManyToOne
private User user;
@NotFound(action=NotFoundAction.IGNORE)
@ManyToOne
private News news;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Hope it helped others, have fun 😉
You save my life!
(D)
Hi DarÃo,
glad it could help 🙂
thank you for the code…
Hi nordine,
anytime, and thank you for your comment.
greate job, work for me
Hi thon.ju,
glad it could helped 🙂
Thnaks a lot..i also got this error and even data was also present in database. But after adding this annotation it works perfectly. Thanks again (Y)
Perfect!!!
Hi …It was a great help …thanx alot
excelent!!
it worked for me cheer!
keep it up..