1

I am working in spring MVC with Hibernate as ORM..

In my module, I am calling a form action to store values into table. But the data is not inserted in that table. But I haven't got any errors in the log.

The code I tried with sessionFactory is,

        String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
                + " values (?,?,?)";
        Connection connections = sessionFactory.getCurrentSession().connection();   
        PreparedStatement stmt = connections.prepareStatement(querys);      
        stmt.setString(1, "Auto");
        stmt.setInt(2, 1);
        stmt.setInt(3, 251);
        stmt.executeUpdate();;
        connections.close();

But in the same way, I can insert the values using JDBC driver as follows,

        private static final String DB_DRIVER = "org.postgresql.Driver";
        private static final String DB_CONNECTION = "jdbc:postgresql://localhost:5432/xxxxx";
        private static final String DB_USER = "xxxxxx";
        private static final String DB_PASSWORD = "xxxxx";

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;     
        String querys = "INSERT INTO reconcile_process "
                + "(process_type,fk_last_modified_by,fk_bank_stmt_id) VALUES"
                + "(?,?,?)";
        try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(querys);
            preparedStatement.setString(1, "Type");
            preparedStatement.setLong(2, 45);   
            preparedStatement.setLong(3, 251);
        preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (dbConnection != null) {
                dbConnection.close();
            }
        }           

        private static Connection getDBConnection() {
            Connection dbConnection = null;
            try {
                Class.forName(DB_DRIVER);
            } catch (ClassNotFoundException e) {
                System.out.println(e.getMessage());
            }
            try {
                dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
                        DB_PASSWORD);
                return dbConnection;
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
            return dbConnection;
        }

Can anyone point me to where the problem is?

DevGo
  • 1,045
  • 1
  • 16
  • 42

3 Answers3

1

Try something like this if you are using Hibernate why not take its advantages

    SessionFactory sessionFactory = 
        new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();  
    txn = session.beginTransaction();
    UrTableClass obj = new UrTableClass ();  
    obj.setDescription("Description");
    obj.setName("NAME");
    obj.setAge(28); 
    session.save(obj); 
    txn.commit();
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • I have a array type column in that table.. So I decided to go with native query using Hibernate.. Cant get way to use the array column in hibernate table... – DevGo Jul 24 '15 at 09:18
  • Even then its easy but its all depend how you setup the things and how much time you have now. – Subodh Joshi Jul 24 '15 at 09:57
  • I have enough time... I am confused to get the array in entity class – DevGo Jul 24 '15 at 10:41
  • Also there is no clear references on how to insert arrays using Hibernate to postgres.. – DevGo Jul 24 '15 at 10:51
  • http://stackoverflow.com/questions/12042181/how-can-i-set-a-string-parameter-to-a-native-query – Subodh Joshi Jul 24 '15 at 10:57
  • In my case array is type of Long... I used that before and not able to insert because it accepts only string arrays... – DevGo Jul 24 '15 at 11:04
  • http://javafreakers.com/how-to-store-array-in-database-in-a-single-column-using-hibernate/ – Subodh Joshi Jul 24 '15 at 11:14
1

Please check in hibernate.cfg.xml file in default package

     <property name="hibernate.connection.autocommit">true</property> 

setting is exist. I think your operation is correct but because of Commit operation You didn't get correct result.

or because of object of domain class which you want to save pass to session factory

session.save(obj); 
txn.commit();
Prabhat Singh
  • 192
  • 20
  • both technique works for annotation based hibernate. – Prabhat Singh Jul 24 '15 at 09:20
  • When you perform reverse engineering from database then hibernate.cfg.xml fill will be generated and database username and password is stored there just add true line to avoid repeation of txn.commit(). – Prabhat Singh Jul 24 '15 at 09:22
  • true is fixed this issue... Is there any other transactions would be affected to set this property? Coz, its setted as false.. – DevGo Jul 24 '15 at 09:49
  • Then after each transaction you perform commit operation as i explain in answer SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); txn = session.beginTransaction(); txn.commit(); – Prabhat Singh Jul 24 '15 at 09:52
  • 1
    https://developer.jboss.org/wiki/Non-transactionalDataAccessAndTheAuto-commitMode – Subodh Joshi Jul 24 '15 at 09:54
  • perfect @SubodhJoshi thank you to explain in short words. – Prabhat Singh Jul 24 '15 at 09:55
0

For finding the root cause of the problem - I suggest starting the server in debug mode. And then using F6 line by line debugging. This will help you track the values getting passed in the objects. If you have been using hibernate,I strongly feel you are missing out some important configurations that are required in hibernate.cfg.xml to save in the database through hibernate .

sTg
  • 4,313
  • 16
  • 68
  • 115