0

I have the following pl/sql query,

INSERT INTO Table(ID,..................) 
VALUES(SEQ.nextval,....................); 
SELECT SEQ.currval ID FROM DUAL;

I need to get ID using hibernate. I am using the following query which showing error,

.....getDataSession().createSQLQuery(hQuery).list()

Any one help me.

5
  • 2
    Go read the documentation about auto-generated IDs. This should be handled automatically by Hibernate. Don't use SQL queries: you're defeating the purpose of using Hibernate. docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single Commented Oct 4, 2011 at 14:14
  • I am not using any class. I am using createSQLQuery. Can you please update my code if possibel or it is impossible with createSQLQuery. I am very new in hibernate. Hopefully you will help me. Commented Oct 4, 2011 at 14:23
  • 1
    Don't use Hibernate as a mean to execute SQL queries. That's not its goal. Use straight JDBC if you want to do that. If you want to use Hibernate, use it as intended, by defining persistent entities and persisting them. Commented Oct 4, 2011 at 14:29
  • OK, Will this means that SEQ.currval will be automatically return? Can you provide me a link about auto generte IDs. Thanks, BTW. Commented Oct 4, 2011 at 14:32
  • 1
    I already did. But here it goes again: docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single Commented Oct 4, 2011 at 14:35

1 Answer 1

1

Create new Object and save it using session.save() method it will return this object id.

 Object object = new Object();
    //add object properties 
    object.setXXX(value);
    //now save the object   
    String id =(String)getDataSession().save(object);

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

I am using createSQLQuery. Can you please tell me where should I place my sql? I am not using any class
Please read @JB Nizen comment he is 100% right :) don't use Hibernate in this case this is ORM application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.