There are 2 Entity class Filials

@Entity @NamedQuery(name="allFilials", query="SELECT f FROM Filials f") public class Filials {@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private String adress; @OneToMany(mappedBy="filials") private List<Opers> opers; public Filials() { super(); stub } ... 

Opers

 @Entity @NamedQuery(name = "allOpers", query = "SELECT o FROM Opers o") public class Opers { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; private String surname; @ManyToOne private Filials filials; public Opers() { super(); // TODO Auto-generated constructor stub } .... 

in DAO insert I do

 @Override public void insert(Opers oper) { getSession().save(oper); } 

insertOper.jsp

 <body> <sf:form modelAttribute="opers" action="${pageContext.request.contextPath}/insertOper" method="post"> <table> <tr> <td>Name</td> <td><sf:input path="name" /></td> </tr> <tr> <td>Surname</td> <td><sf:input path="surname" /></td> </tr> <tr> <td></td> <td><input type="submit" value="Save" /></td> </tr> </table> </sf:form> 

I need that when insert did, he took and ID Filial. In JSP, when you click on a link, it goes to the Opers page and transfers Filials IDs, but with insert-e this ID cannot be entered into the database. How can I enter it?

  • When debugging in the getSession().save(oper); filials oper have filials ? - Sapphiron
  • when debials filials is, but the value is null . And it gets into the database as null , then I have to manually enter the value for it in the database and only after that it is correctly displayed. In Hibernate: insert into Opers (filials_id, name, surname) values (?, ?, ?) logs Hibernate: insert into Opers (filials_id, name, surname) values (?, ?, ?) - Farik013
  • According to the above code it is difficult to say what the problem is. For starters, you can try to specify "not lazy" initialization. @ManyToOne(fetch = FetchType.EAGER) - Sapphiron
  • When opening a page, the controller adds the oper attribute to the model, then a click to go to a new page, on which data is saved to the database by clicking? The data on the pages change? - Sapphiron
  • Again, null . I do not know how to enter it into the database. And where should it take value? When you go to the opers.jsp page, the filials_id goes to the URL, but when you go to the insert.jsp page, the insert.jsp filials_id not go and insert.jsp filials_id no place to take the value. If you need additional code fragments, you tell me which fragment I will add. - Farik013

1 answer 1

When adding an object that has a one-to-many relationship, it is not necessary to write into the object the object to which it belongs, i.e. we write oper , but not filial with all operas. When sending the form, the branch id is not transferred to the controller. There are different options for how this can be implemented.

The most logical in my opinion:

Affiliate page http://yoururl/filial/{filialId} In the controller, we get the filialId. We create a new object in which these forms will be recorded and immediately set the branch ID for it.

  @RequestMapping(value = "/filial/{filialId}", method = RequestMethod.GET) public ModelAndView test(Integer filialId, ModelAndView mav){ Oper oper= new Oper(); oper.setFilialId(filialId); mav.addObject("oper", oper); return mav; } 

Jsp

  <sf:form modelAttribute="oper" action="http://yoururl/insertOper" method="post"> <table> <tr> <td>Name</td> <td><sf:input path="name" /></td> </tr> <tr> <td>Surname</td> <td><sf:input path="surname" /></td> </tr> <tr> <td></td> <td><input type="submit" value="Save" /></td> </tr> </table> </sf:form> 

Then oper with affiliate affiliate will come to http://yoururl/insertOper controller.

  • When you click on the filial , we go to the page with the list of oper s in this branch via the link http://yoururl/opers/{filialId} . But in the class oper I do not have a set method Oper oper= new Oper(); oper.setFilialId(filialId); And the whole problem I think is that I can’t add the filialId in the program. Only in Workbench manually - Farik013
  • Well, add. Of course the setter should be. - Sapphiron
  • How to add a setter for a single column? During initialization, Hibernate creates a Filials object and I have to write an object into it. - Farik013
  • A regular setter that any IDE can generate. Naming classes you knock down. Why Filials , Not Filial ? - Sapphiron
  • Create a Filial object, and represent multiple branches using List or Set . - Sapphiron