使用Hibernate保存到数据库

时间:2017-10-02 15:59:00

标签: java hibernate

我正在尝试将Hibernate集成到我的代码中,但当我尝试保存到数据库时,我得到:

  

org.hibernate.exception.SQLGrammarException:无法执行语句

没有空参数。也许这是因为布尔值是假的,数据库保存0和1?

型号:

    private int id;
private String name;
private Boolean hasProfile7;
private Boolean hasWebCSR;
private Boolean hasWebAdmin;
private Boolean hasWebClient;
private String profile7Branch;
private String webCSRBranch;
private String webAdminBranch;
private String webClientBranch;
private String initialDayEnd;
private String finalDayEnd;
private int amountOfDayEnds;
private String user;

//get sets

获取数据

   //Fetch the data from the GeneralSetup Page
    String name = request.getParameter("name");
    Boolean hasProfile7 = Boolean.valueOf(request.getParameter("profile7"));
    Boolean hasWebCSR = Boolean.valueOf(request.getParameter("webCSR"));
    Boolean hasWebAdmin = Boolean.valueOf(request.getParameter("webAdmin"));
    Boolean hasWebClient = Boolean.valueOf(request.getParameter("webClient"));
    String profile7Branch = request.getParameter("profile7Branch");
    String webCSRBranch = request.getParameter("webCSRBranch");
    String webAdminBranch = request.getParameter("webAdminBranch");
    String webClientBranch = request.getParameter("webClientBranch");
    String initialDayEnd = request.getParameter("initialDayEnd");
    String finalDayEnd = request.getParameter("finalDayEnd");
    //TODO calculate amount of day ends based on the date
    int amountOfDayEnds = 7;
    String days = request.getParameter("amountOfDayEnds");
    if (days != null) {
        amountOfDayEnds = Integer.parseInt(days);
    }
    String user = request.getParameter("user");

//Save to database
    Integer gsID = addGeneralSetup(name,hasProfile7,hasWebCSR,hasWebAdmin,hasWebClient,profile7Branch,webCSRBranch,webAdminBranch,webClientBranch,initialDayEnd,finalDayEnd,amountOfDayEnds,user);

保存数据

public Integer addGeneralSetup(String name, Boolean hasProfile7, Boolean hasWebCSR, Boolean hasWebAdmin,Boolean hasWebClient, String profile7Branch, String webCSRBranch,String webAdminBranch, String webClientBranch,
                               String initialDayEnd, String finalDayEnd, int amountOfDayEnds,String user) {
    Session session = factory.openSession();
    Transaction tx = null;
    Integer generalSetupID = null;
    try{
        tx = session.beginTransaction();
        GeneralSetup generalSetup = new GeneralSetup(name,hasProfile7,hasWebCSR,hasWebAdmin,hasWebClient,profile7Branch,webCSRBranch,webAdminBranch,webClientBranch,initialDayEnd,finalDayEnd,amountOfDayEnds,user);
        generalSetupID = (Integer) session.save(generalSetup);
        tx.commit();
    }catch (HibernateException e) {
        if (tx!=null) tx.rollback();
        e.printStackTrace();
    }finally {
        session.close();
    }
    return generalSetupID;

}

MySQL表generalsetup: enter image description here

0 个答案:

没有答案