www.webdeveloper.com
Results 1 to 2 of 2

Thread: Hibernate insert statement from JSP page

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    72

    Hibernate insert statement from JSP page

    Thanks for your supports. I'm improving in using the frameworks - Hibernate and Spring. My question is very simple: I have a dropdown on a jsp page that is populated from the database thus and a textbox that accepts input to be inserted into database based on the values selected in the dropdown: JSP code:

    Code:
    <tr>
                        <td><form:label path="ownerName" name = "ownerName">Select Overdraft User:</form:label></td>
                        <td><form:select path="ownerName" items = "${addMembersListToDropdown}"></form:select></td>
                    </tr>
                    <tr>
                        <td><form:label path="creditLimit">Enter an Amount:</form:label></td>
                        <td><form:input path="creditLimit"/></td>
                    </tr>
    My quest is this: how do I write the query statement in the Dao class to do this insertion? I am very new to this so I'm inexperienced in calling jsp parameters within the j2ee classes. My sample DAO implementation class:

    Code:
    @Override
        public void saveCyclosUsers(CyclosUsers cyclosUsers) {
            sessionFactory.getCurrentSession().createSQLQuery("INSERT INTO lower_credit ownerName' = +'owner_name');
        }
    The pseudo of the code I intend writing is: "INSERT INTO lower_credit WHERE the value selected from the dropdown = owner_name". Please, kindly support. Appreciate.

  2. #2
    Join Date
    Dec 2013
    Posts
    2
    Hello ken2010
    The idea behind Hibernate is that you do not need to necessarily write your own SQL like statements.
    What you would usually do is to create an instance of the object that you want to persist (make sure the class is annotated correctly!) and then use a session (instantiated by the SessionFactory) and call the save method. Hibernate will do the insert for you!

    See this example:
    Code:
    Stock stock = new Stock();
     stock.setStockId(1);
     Category category = new Category();
    category.setCategoryId(1);
     StockCategory stockCategory = new StockCategory();
    
    stockCategory.setStock(stock); //here you need to get the stock object by id 
    stockCategory.setCategory(category1); //here you need to get the category1 object by id
    stockCategory.setCreatedDate(new Date()); //extra column
    stockCategory.setCreatedBy("system"); //extra column
     session.save(stockCategory );
    (Taken from http://stackoverflow.com/questions/1...bernate-method)
    You could write your own statement, however then refer to HQL.


    Hope this helps.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles