简体   繁体   中英

Java Hibernate Add query

I am facing a problem while trying to persist the existing stock in a preproduction environment. What I am trying to do is actually to loop on a text file and insert substrings from that file into the database.

Here is the class that I execute :

public class RepriseStock { private static Session session;

    public RepriseStock() { 
            session = HibernateUtil.getSessionFactory().openSession(); 
            session.beginTransaction(); 
    } 

    public static int insererPartenaires(String sCurrentLine, int i) { 
            String sql = "INSERT INTO PARTENAIRE(ID," 
                            + "MVTSOC," 
                            + " MVTAGR, " 
                            + "MVTNOMSOC," 
                            + "MVTCPTTMAG," 
                            + "DATEAGREMENT," 
                            + "MVTCHAINE," 
                            + "MVTRGPT," 
                            + "MVTUNION," 
                            + "MVTNOMMAG," 
                            + "MVTTELSOC," 
                            + "MVTADRMAG," 
                            + "MVTVILMAG," 
                            + "MVTMAIL," 
                            + "MVTSITU)" 
                            + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 

            Query query = session.createSQLQuery(sql); 

            query.setInteger(0, i); 

            query.setInteger(1, Integer.parseInt(sCurrentLine.substring(0, 3))); 
            query.setInteger(2, Integer.parseInt(sCurrentLine.substring(3, 10))); 
            query.setString(3, sCurrentLine.substring(10, 34)); 
            query.setInteger(4, Integer.parseInt(sCurrentLine.substring(48, 53))); 

            query.setString(5, sCurrentLine.substring(77, 83)); 

            query.setInteger(6, Integer.parseInt(sCurrentLine.substring(86, 90))); 
            query.setInteger(7, Integer.parseInt(sCurrentLine.substring(90, 94))); 
            // union 
            query.setInteger(8, Integer.parseInt(sCurrentLine.substring(94, 98))); 
            // enseigne 30 
            query.setString(9, sCurrentLine.substring(248, 278)); 
            // tel 
            query.setString(10, sCurrentLine.substring(278, 293)); 
            // adresse 
            query.setString(11, sCurrentLine.substring(293, 323)); 
            // ville 
            query.setString(12, sCurrentLine.substring(323, 348)); 
            // mail 

            query.setString(13, sCurrentLine.substring(398, 448)); 
            // situ 
            query.setString(14, sCurrentLine.substring(449, 452)); 

            return query.executeUpdate(); 

    } 

    /** 
     * @param args 
     */ 
    public static void main(String[] args) { 
            // TODO Module de remplacement de méthode auto-généré 

            BufferedReader br = null; 
            RepriseStock rs = new RepriseStock(); 

            try { 

                    String sCurrentLine; 

                    br = new BufferedReader( 
                                    new FileReader( 
                                                    "C:\\Users\\test\\Desktop\\test\\reprise de stock\\nouveauFichierPREPROD.dat")); 
                    int i = 0; 
                    sCurrentLine = br.readLine(); 
                    while ((sCurrentLine = br.readLine()) != null) { 
                            i++; 

                            RepriseStock.insererPartenaires(sCurrentLine, i); 

                            System.out.println("Nombre de fois : " + i); 

                    } 

                    System.out.println("total (" + i + " )"); 
            } catch (IOException e) { 
                    e.printStackTrace(); 
            } finally { 
                    try { 
                            if (br != null) 
                                    br.close(); 
                    } catch (IOException ex) { 
                            ex.printStackTrace(); 
                    } 
            } 

    } 

}

After the script is executed, i have the total of loops is 1022 times. But the data is not persisted into oracle table (Partenaire)

My log doesn't display any error.

Do you see the issue ?

It looks like you're not committing the transaction.

If you want each update to be a separate transaction, try moving session.beginTransaction(); to the beginning of the insererPartenaires method and capturing the Transaction object returned from that statement in a variable. Then, after each update, make sure to call commit() on the Transaction object.

If you want all of the updates to be the same transaction, move the beginTransaction() and commit() methods to surround the while loop in the main method.


Also just note that you're unnecessarily mixing static and non-static here. Try changing public static int insererPartenaires(String sCurrentLine, int i) to public int insererPartenaires(String sCurrentLine, int i) . Then just use the instantiated RepriseStock object to call the method instead of invoking it statically.

You'll also need to change private static Session session to be private Session session

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM