Salut,
J'arrive pas à ajouter un objet fils:
j'ai deux entité: Parent et Child.
J'aimerai ajouter un nouveau Child pour un Parent donner.
voila ce que j'ai dejà essayer:
Entité Parent
Entité Child
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 public class Parent { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int parentId; private String parentName; @OneToMany(mappedBy="parent") private List<Child> children = new ArrayList<Child>(); ... public void addChildToParent(Child c) { c.setParent(this); children.add(c); } }
Servlet
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 public class Child { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int childId; private String childName; @ManyToOne private Parent parent; ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 String childName = request.getParameter("childName"); int parentId = Integer.parseInt(request.getParameter("parentId")); Child newChild = new Child(); Parent ParentOfChild = new Parent(); newChild.setChildName(childName); ParentOfChild.addChildToParent(newChild); ParentOfChild = parentDao.getParent(parentId); parentDao.editParent(ParentOfChild); ...
Partager