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.

jatin_ghataliya
  • 144
  • 1
  • 1
  • 16

1 Answers1

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.

Community
  • 1
  • 1
m.aibin
  • 3,528
  • 4
  • 28
  • 47