使用onetomany从表单中插入数据库中的数据也会根据所选值在行中插入多个seatNo

时间:2016-08-09 16:11:24

标签: java html spring

我需要在数据库中插入表单中的数据...也可以使用onetomany映射,这样我就可以插入bus seatno的多个行值。用户应该能够从addbus.jsp的下拉框中选择总座位数,并根据所选值将数据插入sideA表中。如果用户在下拉选项中没有选择4,则在sideA表中1,2,3和4值应插入不同行的seatNo列中

  

Bus.java

package edu.abc.test.models;    
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "addbus")
public class Bus {
    @Id
    @GeneratedValue
    @Column(name = "Bus_ID")
    private Long id;
    @Column(name = "Bus_No")
    private String busNo;
    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "bus_ID")
    private List<SideA> sideA;  
    /*Getters and setters*/
  }
  

SideA.java

package edu.abc.test.models;    
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "sideA")
public class SideA {
    @Id
    @GeneratedValue
    @Column(name = "sideA_ID")
    private Long id;
    @ManyToOne
    @JoinColumn(name = "bus_ID", insertable = false,  updatable = false,nullable = false)
    private Bus bus;
    @Column(name = "Seat_No")
    private int seatNo;
    @Column(name = "Status",columnDefinition = "enum('Available','Taken')             default 'Available'")
    @Enumerated(value = EnumType.STRING)
    private Status status = Status.Available;
    /*Getter and setters*/
    public enum Status{
        Available,
        Taken
    }
}
  

BusController.java

package edu.abc.test;    
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.abc.test.daos.BusDao;
import edu.abc.test.models.Bus;

@Controller
public class BusController {
    //private static final Logger logger = LoggerFactory.getLogger(BusController.class);

    @Autowired
    private BusDao busDao;

    @RequestMapping(value = "adBus",method = RequestMethod.GET)
    public String addBus(Model model){
        model.addAttribute("bus",new Bus());
        return "addBus";
    }

    @RequestMapping(value = "adBus", method = RequestMethod.POST)
    public String insert(@ModelAttribute Bus bus,Model model){
        busDao.insert(bus);
        model.addAttribute("bus",new Bus());
        return "addBus";
    }
}
  

BusDao.java

package edu.abc.test.daos;    
import java.util.List;    
import edu.abc.test.models.Bus;    

public interface BusDao {
    public Long insert(Bus bus);
    public List<Bus>getAll();
    public Bus get(Long id);
}

BusDaoImpl.java

package edu.abc.test.daos;    
import java.util.List;    
import javax.annotation.Resource;    
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.abc.test.models.Bus;

@Repository
public class BusDaoImpl implements BusDao{
    @Resource
    private SessionFactory sessionFactory;

    @Override
    @Transactional
    public Long insert(Bus bus) {
        Session session = sessionFactory.getCurrentSession();
        Long id = (Long) session.save(bus);
        return id;
    }

    @Override
    public List<Bus> getAll() {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Bus.class);
        List<Bus> busList = criteria.list();
        return busList;
    }

    @Override
    @Transactional
    public Bus get(Long id) {
        Session session = sessionFactory.getCurrentSession();
        Bus bus = (Bus) session.get(Bus.class, id);
        return bus;
    }

}
  

addBus.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Bus</title>
</head>
<body>
    <div class = "body">
        <form:form method="POST" modelAttribute="bus">
            <table>
                <tr>
                    <td colspan ="2"><h2>Add Bus</h2></td>
                </tr>
            <tr>
                <td> <form:label path="busNo">Bus No</form:label> </td>
                <td> <form:input path="busNo"/> </td>
            </tr>
            <tr>
                <td> <form:label path="sideA.seatNo">Side A1</form:label> </td>
                <td> <form:select path="sideA.seatNo">
                        <form:option value="6">6</form:option>  
                        <form:option value="10">10</form:option>    
                        <form:option value="12">12</form:option>    
                        <form:option value="14">14</form:option>
                        <form:option value="16">16</form:option>        
                     </form:select> </td>
            </tr>
            <tr>
            <td colspan="2"> <input type="submit" value = "Save"> </td>
            </tr>
        </table>
    </form:form>
    </div>
</body>
</html>

0 个答案:

没有答案