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.
|