2

I want to know how to store the hibernate update query with model class and evict method in the string.

If we set the show_sql property to true, it displays the SQL query onside console, but my requirements like to store the insert query inside the DB. I want to store the query in string with parameter.

Following is the code.

final SessionFactory factoryNew =  configHibernate.configure().buildSessionFactory();
hibernateSession = factoryNew.openSession();
final Transaction insertSerialIdTransaction = hibernateSession.beginTransaction();
                final GroupDevice addGroupDevice = new GroupDevice();
                addGroupDevice.setDeviceId(0);
                addGroupDevice.setSerialId(cut[0]);
                addGroupDevice.setOldMsata_password(oldMsataPassword);
                addGroupDevice.setIsNew(0);
                addGroupDevice.setDeviceStatus("staging");
                hibernateSession.save(addGroupDevice);
                hibernateSession.flush();
                insertSerialIdTransaction.commit();

Show the query on console like this: INSERT Into GroupDevice (DeviceId,SerialId,OldMsata_password, IsNew, DeviceStatus) values(0,"kdjhg65",0,"staging");

I want to store the above query in the string, how to store in the string.?

Thanks in advance.

1 Answer 1

3

Hibernate show_sql is printing directly to console (System.out), so you can capture that output by using the System.setOut method to change System.out to go to a PrintStream provided by you. If you create a PrintStream connected to a ByteArrayOutputStream, then you can capture the output as a String.

Look here: Redirect console output to string in java

Then you can easily do what you want to with that.

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

3 Comments

Thanks for your answer and time. i got almost idea from your above link. from there i will do it. Thank you so much...:)
If you like my answer, please accept and upvote it ;)
oooppppsss, i don't have 15 reputation to upvote,sorry for right now, but whenever i got more than 15 reputation,it will be voted..:)

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.