TransactionException:"交易未成功启动"

时间:2014-03-27 12:30:14

标签: java mysql spring hibernate

对不起我的英语。

我有这段代码:

@Override
public String insertMessage(Message message) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.save(message);

    Query query = session.createSQLQuery("SELECT last_insert_id() as id FROM message group by 'id'");
    BigInteger ultimoID = (BigInteger)query.uniqueResult();
    String id = ultimoID.toString();
    return id;
} 

在这里,我在表中插入一条消息,但我不想提交...之后转到spring控制器,我的代码到达此方法:

@Override
public void insertLayouts(int idLastMessage, List<Information_type> infTypes, List<String> values) {
    Session session = sessionFactory.getCurrentSession();        
    try{
        for (int i = 0; i < infTypes.size(); i++) {
            String tipo = infTypes.get(i).getType_id();

            if (tipo.equalsIgnoreCase("varchar")) {
                Tbl_Varchar tbl_Varchar = new Tbl_Varchar();

                tbl_Varchar.setId_msg(idLastMessage);
                tbl_Varchar.setId_inf_type(infTypes.get(i).getInf_type());
                tbl_Varchar.setValue(values.get(i));

                session.save(tbl_Varchar);
            } else if (tipo.equalsIgnoreCase("int")) {
                Tbl_Int tbl_Int = new Tbl_Int();

                tbl_Int.setId_msg(idLastMessage);
                tbl_Int.setId_inf_type(infTypes.get(i).getInf_type());
                tbl_Int.setValue(Integer.parseInt(values.get(i)));

                session.save(tbl_Int);

            } else if (tipo.equalsIgnoreCase("double")) {
                Tbl_Double tbl_Double = new Tbl_Double();

                tbl_Double.setId_msg(idLastMessage);
                tbl_Double.setId_inf_type(infTypes.get(i).getInf_type());
                tbl_Double.setValue(Double.parseDouble(values.get(i)));

                session.save(tbl_Double);

            } else if (tipo.equalsIgnoreCase("date")) {
                Tbl_Date tbl_Date = new Tbl_Date();

                tbl_Date.setId_msg(idLastMessage);
                tbl_Date.setId_inf_type(infTypes.get(i).getInf_type());
                try {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
                    Date fecha = simpleDateFormat.parse(values.get(i));
                    tbl_Date.setValue(fecha);
                } catch (Exception ex) {
                    throw new RuntimeException();
                }
                session.save(tbl_Date);

            } else if (tipo.equalsIgnoreCase("text")) {
                Tbl_Text tbl_Text = new Tbl_Text();

                tbl_Text.setId_msg(idLastMessage);
                tbl_Text.setId_inf_type(infTypes.get(i).getInf_type());
                tbl_Text.setValue(values.get(i));

                session.save(tbl_Text);
            }
        }
        session.getTransaction().commit();
    }catch(Exception ex){
        session.getTransaction().rollback();
        throw new RuntimeException();
    }finally{
        session.close();
    }
}

这里在表中插入其他数据,现在我进行提交但是却抛出了这个错误。有什么想法吗?

0 个答案:

没有答案