I'm coding a Human Resource Management System and I'm trying to make process of adding CV. In backend (Java) I got a ResumeHead class that includes fields of id, coverLetter, createdAt etc. and also it has a field "private List resumeLanguage;" like that. With that way I tried to create ResumeLanguage more than one when we're adding resume. So, ResumeLanguage class has id of ResumeHead. So, here is the problem : In frontend(React) O want to add a resume I need to add ResumeHead first and then adding ResumeLanguage. For adding ResumeLanguage, I need to id of ResumeHead. How can I take the id of ResumeHead after I added ResumeHead directly ?
Asked
Active
Viewed 62 times
0
-
What do you use for persistence on backend? JPA, DAO pattern, or no-sql databases? – bichoFlyer Jul 19 '21 at 02:47
-
I'm Using JPA And PostgreSQL – Zambak Lale Jul 19 '21 at 06:25
1 Answers
0
In JPA, you need to create a List<ResumeLanguage>
with the @OneToMany
annotation in the ResumeHead entity. Then, you add the ResumeLanguage objects to the list, to persist invoke entityManager.persist(head);
(don't forget to use a JPA transaction) and that's all. The method should look something like this:
void mymethod(){
var head = getResumeHeadFromUI();
head.setResumeLanguages(getResumeLanguagesFromUI());
var em = someMethodToGetEntityManager();
try{
em.getTransaction().begin();
em.persist(head);
em.getTransaction().commit();
}catch(PersistenceException e){
if(em.getTransaction().isActive()){
em.getTransaction().rollback();
}
throw e;
}
}
In this way, you don't need to specify the ResumeHead ID in any place, JPA will take care of it.

bichoFlyer
- 178
- 9
-
Actually, I'm new in JPA And I don't know what is entityManager.persist(); method and Where am I going to write this method ? – Zambak Lale Jul 19 '21 at 14:27
-
In JPA, you don't work with JDBC nor SQL queries, instead you use a PersistenceUnit, which provides you with an EntityManagerFactory, which creates an EntityManager. The EntityManager is responsable of generating the necessary SQL queries for an insert, update or delete each time you invoke EntityManager.persist, merge or delete. Take a look at this tutorial: https://www.tutorialspoint.com/jpa/index.htm – bichoFlyer Jul 19 '21 at 15:17