1
String query = ("insert into complaint(name,issue,whenadded)  
                VALUES('$name','$issue',now())" );

This is my query.i am using strut-hibernate. how to execute the above query ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

1

Telling from your question you are very new to Hibernate. Hibernate will automatically insert any entity objects which have been modified during the current session. So to carry out your INSERT in Hibernate, you can create a Complaint POJO, assign the three fields you want, and then save the session.

Complaint complaint = new Complaint();
stockTran.setName("A complaint");
stockTran.setIssue("Hibernate SO question");
stockTran.setWhenAdded(new Date());

session.save(complaint);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • i want time in IST.this is my code.Session Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("IST")); // Calender Instance System.out.println("Current time :"+c1.getTime()); // getting time in GMT Date date=new Date(); SimpleDateFormat sdf= new SimpleDateFormat(); sdf.setTimeZone(TimeZone.getTimeZone("IST")); System.out.println("IST : "+sdf.format(date)); complaint.setDate(new Date()); session.save(complaint); transaction.commit(); return "success" ; –  May 11 '15 at 07:09
  • what to put in setDate argument to get time in IST complaint.setDate (); –  May 11 '15 at 07:11
0

Using Hibernate you can save a whole object, you don't need to fire insert into xyz values(x,x) query. If you use it, then Hibernate is worthless. But you must have to do mapping of table with POJO (complaint) via XML or annotation configuration.

You should write this code snap:

Session session = sessionFactory.openSession();

Complaint complaintObject = new Complaint();
complaintObject.setName(<Name_of_complaint>);
complaintObject.setIssue(<issue_detail>);
complaintObject.setWhenAdded(new Date());

session.beginTransaction();
session.save(complaint);
session.getTransaction().commit();

Regarding timezone issue :

public static Date convertLocalToISTTimezone( Date localDate )
    {
        Date datetoReturn = null;
        try
        {
            setLog("*%" + "On " + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()) + " GMT **** ENTER  :" + localDate);

            TimeZone timezone = TimeZone.getTimeZone('<set_timezone_where _your server_will_be>');

            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

            sdf.setTimeZone(timezone);

            SimpleDateFormat sdfgmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

            TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT+05:30");
            sdfgmt.setTimeZone(gmtTimeZone);

            SimpleDateFormat dateFormatLocal = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            datetoReturn = dateFormatLocal.parse(sdfgmt.format(sdf.parse(sdf.format(localDate))));

            setLog("*%" + "On " + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()) + " GMT **** EXIT :" + datetoReturn);

        }
        catch ( ParseException e )
        {
            e.printStackTrace();
        }
        return datetoReturn;
    }
Rajnikant Patel
  • 561
  • 3
  • 19