A Weird Hibernate Exception : org.hibernate.ObjectNotFoundException: No row with the given identifier exists

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

Google+

9 Comments

Darío

about 5 years ago

You save my life! (D)

Reply

edwin

about 5 years ago

Hi Darío, glad it could help :)

nordine

about 5 years ago

thank you for the code...

Reply

edwin

about 5 years ago

Hi nordine, anytime, and thank you for your comment.

thon.ju

about 4 years ago

greate job, work for me

Reply

edwin

about 4 years ago

Hi thon.ju, glad it could helped :)

Arnab Layek

about 5 months ago

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)

Reply

Robson

about 4 months ago

Perfect!!!

Reply

mina

about 2 months ago

Hi ...It was a great help ...thanx alot

Reply

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)