-->
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.  [ 4 posts ] 
Author Message
 Post subject: Error saving a object using getCurrentSession().save(obj)
PostPosted: Tue Mar 15, 2011 10:07 am 
Newbie

Joined: Wed Feb 16, 2011 1:56 pm
Posts: 7
I'm trying to persist a object with the following code:

Code:
sessionFactory.getCurrentSession().save(myObject)


myObject is a instance of Class MyObject & most of the fields are set from Spring form. Only the columns which have default values defined in MySQL are not set e.g. ID

Code:
@Id
   @GeneratedValue(strategy = IDENTITY)
   @Column(name = "MyObjectID", unique = true, nullable = false)
   public Integer getMyObjectID() {
      return this.myObjectID;
   }

There are other fields too like current_TS which has default in MYSQL.

But upon execution i get the following error:
Code:
org.hibernate.AssertionFailure: null id in .............. entry (don't flush the Session after an exception occurs)


How to persist an object without setting it's default values so that MySQL sets those default values. Please guide.
I'm stuck...please help.


Top
 Profile  
 
 Post subject: Re: Error saving a object using getCurrentSession().save(obj)
PostPosted: Tue Mar 15, 2011 10:56 am 
Newbie

Joined: Tue Feb 01, 2011 2:06 am
Posts: 8
I just implemented auto id using Hibernate using MySQL with no problems.

Is the column name in the database for myObject really "MyObjectId"; that doesn't seem like a typical column name, you may want to double check that.

The only times -- of which I am aware -- that mysql will automatically set a column is with an auto increment or timestamp column.


Top
 Profile  
 
 Post subject: Re: Error saving a object using getCurrentSession().save(obj)
PostPosted: Tue Mar 15, 2011 1:22 pm 
Newbie

Joined: Wed Feb 16, 2011 1:56 pm
Posts: 7
The issue is resolved now. Thanks a lot.


Top
 Profile  
 
 Post subject: Re: Error saving a object using getCurrentSession().save(obj)
PostPosted: Tue Mar 15, 2011 1:50 pm 
Newbie

Joined: Fri Dec 24, 2010 2:08 pm
Posts: 6
Hi,

Mapping of Id generation seems fine, you can also use as
@Id
@GeneratedValue
@Column(name="id")
public Integer getId() {
return id;
}

Where Id will be primaryKey declared as
id INT PRIMARY KEY AUTO_INCREMENT

You can try using new session for the same purpose if current session has been clear / closed by some thread.
SessionFactory().openSession()

If you are using Hibernate with Spring, we can always use HibernateTemplate
getHibernateTemplate().getSessionFactory().getCurrentSession()


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.