-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: EntityManager.find() is not able to find row by primary Id
PostPosted: Sat Jan 02, 2016 1:36 pm 
Newbie

Joined: Sat Jan 02, 2016 1:26 pm
Posts: 2
I am newbie to hibernate. It's very simple program i have written, but not able to figure it out where the things are going wrong.
This is my Entity class:-

package org.neeraj.walmart.dto;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetails {

@Id
private int userid;

private String userName;
private Date joinedDate;

private String Address;
private String description;

public int getUserId() {
return userid;
}
public void setUserId(int userId) {
this.userid = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getJoinedDate() {
return joinedDate;
}
public void setJoinedDate(Date joinedDate) {
this.joinedDate = joinedDate;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

this is my Hibernate test file to persist and retrive the data from informix DB.

package org.neeraj.walmart;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.RollbackException;

import org.neeraj.walmart.dto.UserDetails;

public class HibernateTest {

public static void main(String[] args) {

UserDetails user1 = new UserDetails();
user1.setUserId(2);
user1.setUserName("First User");
user1.setJoinedDate(new Date());
user1.setAddress("User1 address");
user1.setDescription("User1 description is present here");


EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernateLearningContext");
//Code snippet to save the object to DB
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
em.persist(user1);
em.getTransaction().commit();
} catch (RollbackException ex) {
em.getTransaction().rollback();
} finally {
em.close();
}

// code snippet to retrieve the object
UserDetails user1Copy = null;
em = emf.createEntityManager();
try {
em.getTransaction().begin();
user1Copy = em.find(UserDetails.class, 2); // This method finds by primary key
if (user1Copy == null) {
System.out.println(" data not found");
} else {
System.out.println("user name retrived is " + user1Copy.getUserName());
}

} catch (Exception ex) {

} finally {
em.close();
}
}
}

And this is stack trace :-

INFO: HHH000227: Running hbm2ddl schema export
Hibernate:
drop table UserDetails
Jan 02, 2016 10:55:39 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table UserDetails
Jan 02, 2016 10:55:39 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: The specified table (userdetails) is not in the database.
Hibernate:
create table UserDetails (
userid integer not null,
Address varchar(255),
description varchar(255),
joinedDate datetime year to fraction(5),
userName varchar(255),
primary key (userid)
)
Jan 02, 2016 10:55:39 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate:
insert
into
UserDetails
(Address, description, joinedDate, userName, userid)
values
(?, ?, ?, ?, ?)
Hibernate:
select
userdetail0_.userid as userid1_0_0_,
userdetail0_.Address as Address2_0_0_,
userdetail0_.description as descript3_0_0_,
userdetail0_.joinedDate as joinedDa4_0_0_,
userdetail0_.userName as userName5_0_0_
from
UserDetails userdetail0_
where
userdetail0_.userid=?
Jan 02, 2016 10:55:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: -79738, SQLState: IX000
Jan 02, 2016 10:55:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
[b]ERROR: No such column name
Jan 02, 2016 10:55:53 PM org.hibernate.event.internal.DefaultLoadEventListener onLoad
INFO: HHH000327: Error performing load command : org.hibernate.exception.GenericJDBCException: Could not read entity state from ResultSet : EntityKey[org.neeraj.walmart.dto.UserDetails#2]


I have used hbm2ddl_auto to "create". so everytime i run the application, table drops and recreated, persist the data and then i try to retrieve it. When it is trying to retrive it, it is throwin an error
saying "Error" no such column name". I have spent like half day to find out the root cause of this problem, but bad luck.
This is a very simple snippet of code. dont know where things are going wrong. Data is being persisted successfully. i checked it. only problem is while retrieving.


Top
 Profile  
 
 Post subject: Re: EntityManager.find() is not able to find row by primary Id
PostPosted: Thu Feb 25, 2016 10:33 am 
Newbie

Joined: Thu Feb 25, 2016 10:23 am
Posts: 1
Is this issue solved ?

Can you print the stack trace here ?
Code:
} catch (Exception ex) {

}

This will give some more information about the error.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.