-->
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: Hibernate session.get returns null
PostPosted: Tue Dec 15, 2015 12:42 pm 
Newbie

Joined: Wed Sep 16, 2015 11:40 am
Posts: 3
Hi,

I have the below two lines of code to get the record from the table but returning null in spite of record present in the table. Just before running the below code I ensured that the data inserted successfully into the database.

1) inserted the data successfully into MyData Table

public boolean insertUpdateXMLintoMyData(Long key,String messageId)throws Exception
{

Session session = sessionFactory.getCurrentSession();
MyData myData = new MyData();
myData .setKey(key);
myData .setMessageId(messageId);
session.saveOrUpdate(myData );
return true;
}

2) Fetching the data to update but returns null in spite of data present in the database.

Session session = sessionFactory.getCurrentSession();
MyData data = (MyData) session.get(MyData.class,Key,LockMode.UPGRADE);


What could be the reason.

Thanks in Advance.


Top
 Profile  
 
 Post subject: Re: Hibernate session.get returns null
PostPosted: Wed Dec 16, 2015 2:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You need to post the mappings of all the entities and the actual logic that does the persistence.
There's too little information to find out what it's not working.


Top
 Profile  
 
 Post subject: Re: Hibernate session.get returns null
PostPosted: Wed Dec 16, 2015 3:45 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Try this

Code:
public void insertUpdateXMLintoMyData(Long key,String messageId)throws Exception {
    Session session = sessionFactory.getCurrentSession();
    MyData myData = new MyData();
    myData .setKey(key);
    myData .setMessageId(messageId);
    Transaction tx = session.beginTransaction();
    try {
        session.saveOrUpdate(myData );
    } catch (Exception ex) {
        tx.rollback();
        throw ex;
    }
    tx.commit(); //very important!!!
}


Top
 Profile  
 
 Post subject: Re: Hibernate session.get returns null
PostPosted: Wed Dec 16, 2015 3:48 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
session.saveOrUpdate only add your entity object into the level1 memory cache of current session, the database is not changed, that why nothing can be found.

The change of session level cache must be uploaded to database by
(1)org.hibernate.Transaction.commit()
or
(2) org.hibernate.Session.flush()


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.