Ajuda com chekbox, Many To Many, Servlet e Hibernate
Olá pessoal, preciso receber diversos checkbox values do meu form e enviar as informações para o Sensor DAO mas não faço ideia de como fazer, busquei diversos tutoriais e não encontrei nenhum que pudesse me ajudar, estou deixando todas as classes logo abaixo.
FORM
SENSOR SERVLET - É aqui que preciso receber as informações do form e inserir no DAO.
SENSORS
SENSOR DAO
STIMULUS
OBSERVABLE PROPERTY
SYSTEM
FORM
<form class="card" action="newSensor" method="post"> <div class="modal-body"> <div class="col-sm-12 col-md-12"> <div class="form-group"> <label class="form-label">Sensor name</label> <input type="text" name="name" class="form-control" placeholder="Enter sensor name"> </div> </div> <div class="col-sm-12 col-md-12"> <div class="form-group"> <label class="form-label">Sensor description</label> <input type="text" name="description" class="form-control" placeholder="Enter sensor description"> </div> </div> <div class="col-sm-12 col-md-12"> <div class="form-group"> <label class="form-label">Stimulus</label> <div class="custom-controls-stacked"> <% StimulusDao stiDao = new StimulusDao(); List<Stimulus> ls = stiDao.getList(); for (Stimulus s : ls) { %> <label class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" name="stimulus" value="<%=s.getId()%>"> <span class="custom-control-label"><%=s.getValue()%></span> </label> <%}%> </div> </div> </div> <div class="col-sm-12 col-md-12"> <div class="form-group"> <div class="form-label">Observable Property</div> <div class="custom-controls-stacked"> <% ObservablePropertyDao obsDao = new ObservablePropertyDao(); List<ObservableProperty> lo = obsDao.getListOfObservable(); for (ObservableProperty o : lo) { %> <label class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" name="obsProperty" value="<%=o.getPropertyId()%>"> <span class="custom-control-label"><%=o.getPropertyName()%></span> </label> <%}%> </div> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="submit" class="btn btn-primary">Register</button> </div> </form>
SENSOR SERVLET - É aqui que preciso receber as informações do form e inserir no DAO.
private void newSensor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // VALUES FORM String name = request.getParameter("name"); String description = request.getParameter("description"); String[] stimulus = request.getParameterValues("stimulus"); String[] observab = request.getParameterValues("obsProperty"); }
SENSORS
@Entity @Table(name = "tbl_sensors") @PrimaryKeyJoinColumn public class Sensors extends System implements Serializable { @ManyToMany private Set<ObservableProperty> observableProperties = new HashSet<ObservableProperty>(); @ManyToMany private Set<Stimulus> stimulus = new HashSet<Stimulus>(); public Sensors() { } public Sensors(String systemName, String systemDescription, Set<ObservableProperty> observableProperties, Set<Stimulus> stimulus){ super(systemName, systemDescription); this.observableProperties = observableProperties; this.stimulus = stimulus; }
SENSOR DAO
public boolean registerSensor(Sensors sensor, Set observableProperties, Set stimulus) { Session sessions = NewHibernateUtil.getSessionFactory().openSession(); if (isSensorExists(sensor)) { return false; } Transaction tx = null; try { tx = sessions.getTransaction(); tx.begin(); sessions.saveOrUpdate(sensor); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { sessions.close(); } return true; }
STIMULUS
public class Stimulus { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private int value; @ManyToMany(cascade={ CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE }) private Set<ObservableProperty> observableProperties = new HashSet<ObservableProperty>(); public Stimulus() { } public Stimulus(int value, Set<ObservableProperty> observableProperties){ this.value = value; this.observableProperties = observableProperties; }
OBSERVABLE PROPERTY
@Entity @Table(name = "tbl_observable_property") @PrimaryKeyJoinColumn public class ObservableProperty extends Property implements Serializable { public ObservableProperty(){ } public ObservableProperty(String name, String description, FeatureOfInterest featureOfInterest) { super(name, description, featureOfInterest); } public ObservableProperty(Long id, String name, String description, FeatureOfInterest featureOfInterest) { super(id, name, description, featureOfInterest); } }
SYSTEM
@Entity @Table(name = "tbl_system") @Inheritance(strategy = InheritanceType.JOINED) public class System implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long systemId; private String systemName; private String systemDescription; //@ManyToOne private System superSystem; public System() { } public System(String systemName, String systemDescription){ this.systemName = systemName; this.systemDescription = systemDescription; }
Endreo Stange
Curtidas 0