diff --git a/src/main/java/ch/hearc/odi/koulutus/business/Course.java b/src/main/java/ch/hearc/odi/koulutus/business/Course.java index 750ba10..ba7e731 100644 --- a/src/main/java/ch/hearc/odi/koulutus/business/Course.java +++ b/src/main/java/ch/hearc/odi/koulutus/business/Course.java @@ -1,5 +1,6 @@ package ch.hearc.odi.koulutus.business; +import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonManagedReference; import java.io.Serializable; import java.util.ArrayList; @@ -113,6 +114,7 @@ public void setParticipants(List participants) { } @ManyToOne + @JsonBackReference @IndexColumn(name="Programs") public Program getProgram() { return program; diff --git a/src/main/java/ch/hearc/odi/koulutus/business/Program.java b/src/main/java/ch/hearc/odi/koulutus/business/Program.java index 713c3bd..67b33b5 100644 --- a/src/main/java/ch/hearc/odi/koulutus/business/Program.java +++ b/src/main/java/ch/hearc/odi/koulutus/business/Program.java @@ -1,5 +1,6 @@ package ch.hearc.odi.koulutus.business; +import com.fasterxml.jackson.annotation.JsonManagedReference; import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -69,6 +70,7 @@ public void setPrice(int price) { } @OneToMany(targetEntity = Course.class, fetch = FetchType.EAGER) + @JsonManagedReference public List getCourses() { return courses; } diff --git a/src/main/java/ch/hearc/odi/koulutus/rest/ProgramResource.java b/src/main/java/ch/hearc/odi/koulutus/rest/ProgramResource.java index 61ed754..7a56a3b 100644 --- a/src/main/java/ch/hearc/odi/koulutus/rest/ProgramResource.java +++ b/src/main/java/ch/hearc/odi/koulutus/rest/ProgramResource.java @@ -85,7 +85,11 @@ public Course addCourse(@PathParam("programId") Long programId, Course course) { Program program = new Program(); program.setId(programId); course.setProgram(program); + try { return persistenceService.createAndPersistCourse(programId, course); + } catch ( ProgramException e){ + throw new WebApplicationException(e.getMessage()); + } } @GET diff --git a/src/main/java/ch/hearc/odi/koulutus/services/PersistenceService.java b/src/main/java/ch/hearc/odi/koulutus/services/PersistenceService.java index 9b07517..5cee71d 100644 --- a/src/main/java/ch/hearc/odi/koulutus/services/PersistenceService.java +++ b/src/main/java/ch/hearc/odi/koulutus/services/PersistenceService.java @@ -200,14 +200,20 @@ public List getCoursesByProgramId(Long programId) { return (ArrayList) courses; } - public Course createAndPersistCourse(Long programId, Course course) { + public Course createAndPersistCourse(Long programId, Course course) throws ProgramException{ //TODO: fix that - Program program = new Program(); - program.setId(programId); - course.setProgram(program); + EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); + Program program = entityManager.find(Program.class, programId); + if (program == null) { + logger.error(" program with id " + program.getId() + " not found"); + throw new ProgramException(" Program not found"); + } + course.setProgram(program); + program.getCourses().add(course); entityManager.persist(course); + //entityManager.persist(program); entityManager.getTransaction().commit(); entityManager.close(); logger.info("course with id" + course.getId() + " created" ); diff --git a/src/test/java/ch/hearc/odi/koulutus/services/PersistenceServiceTest.java b/src/test/java/ch/hearc/odi/koulutus/services/PersistenceServiceTest.java index b97dcfd..c42f634 100644 --- a/src/test/java/ch/hearc/odi/koulutus/services/PersistenceServiceTest.java +++ b/src/test/java/ch/hearc/odi/koulutus/services/PersistenceServiceTest.java @@ -55,7 +55,7 @@ public void createAndPersistAParticipant() throws ParticipantException { } @Test - public void createAndPersistACourse() { + public void createAndPersistACourse() throws Exception{ Course course = new Course(); //TODO: fix enum? course.setQuarter(QuarterEnum.NUMBER_2);