From e0cd6f12fc04f4eff7c0e465128cee1673e12d3f Mon Sep 17 00:00:00 2001 From: nils Date: Sun, 20 Aug 2017 20:44:41 +0200 Subject: [PATCH] add Course entity and jpa persist --- src/main/java/Java/domain/CourseDomain.java | 56 +++++++++++++++ src/main/java/Java/ejb/CourseService.java | 20 ++++++ src/main/java/Java/ejb/CourseServiceImpl.java | 69 +++++++++++++++++++ src/main/java/Java/jpa/Course.java | 63 +++++++++++++++++ src/main/resources/META-INF/persistence.xml | 14 ++-- 5 files changed, 213 insertions(+), 9 deletions(-) create mode 100644 src/main/java/Java/domain/CourseDomain.java create mode 100644 src/main/java/Java/ejb/CourseService.java create mode 100644 src/main/java/Java/ejb/CourseServiceImpl.java create mode 100644 src/main/java/Java/jpa/Course.java diff --git a/src/main/java/Java/domain/CourseDomain.java b/src/main/java/Java/domain/CourseDomain.java new file mode 100644 index 0000000..e55da57 --- /dev/null +++ b/src/main/java/Java/domain/CourseDomain.java @@ -0,0 +1,56 @@ +package domain; + +/** + * Created by Nils Gerstner on 8/20/17. + */ +public class CourseDomain { + private long id; + private String name; + private String description; + private String courseNr; + + public CourseDomain(String name, String description, String courseNr){ + this.name = name; + this.description = description; + this.courseNr = courseNr; + } + + public CourseDomain(long id, String name, String description, String courseNr){ + this.id = id; + this.name = name; + this.description = description; + this.courseNr = courseNr; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCourseNr() { + return courseNr; + } + + public void setCourseNr(String courseNr) { + this.courseNr = courseNr; + } +} \ No newline at end of file diff --git a/src/main/java/Java/ejb/CourseService.java b/src/main/java/Java/ejb/CourseService.java new file mode 100644 index 0000000..428627b --- /dev/null +++ b/src/main/java/Java/ejb/CourseService.java @@ -0,0 +1,20 @@ +package ejb; + +import domain.CourseDomain; + +import javax.ejb.Local; +import java.util.List; + +/** + * Created by Solidbeans on 2017-03-20. + */ +@Local +public interface CourseService { + + void addCourse(CourseDomain course); + void updateCourse(CourseDomain course); + void removeCourse(Long id); + CourseDomain getCourse(Long id); + List getCourses(); + public List getCoursesContaining(String filter); +} diff --git a/src/main/java/Java/ejb/CourseServiceImpl.java b/src/main/java/Java/ejb/CourseServiceImpl.java new file mode 100644 index 0000000..c642901 --- /dev/null +++ b/src/main/java/Java/ejb/CourseServiceImpl.java @@ -0,0 +1,69 @@ +package ejb; + +import domain.CourseDomain; +import jpa.Course; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Created by Solidbeans on 2017-03-20. + */ + +@Stateless +public class CourseServiceImpl implements CourseService{ + @PersistenceContext + EntityManager em; + + @Override + public void addCourse(CourseDomain course) { + Course c = new Course(course.getName(),course.getDescription(),course.getCourseNr()); + em.persist(c); + + } + + @Override + public void updateCourse(CourseDomain course) { + Course c = em.find(Course.class,course.getId()); + c.setName(course.getName()); + c.setDescription(course.getDescription()); + c.setCourseNr(course.getCourseNr()); + em.merge(c); + } + + @Override + public void removeCourse(Long id) { + Course c = em.find(Course.class, id); + em.remove(c); + } + + @Override + public CourseDomain getCourse(Long id) { + Course c = em.find(Course.class,id); + CourseDomain cd = new CourseDomain(c.getId(),c.getName(),c.getDescription(),c.getCourseNr()); + + return cd; + } + + @Override + public List getCourses() { + List courseList = em.createNamedQuery("selectAll").getResultList(); + + return courseList.stream(). + map(c->new CourseDomain(c.getId(),c.getName(),c.getDescription(),c.getCourseNr())). + collect(Collectors.toList()); + } + + @Override + public List getCoursesContaining(String filter) { + List courseList = em.createNamedQuery("selectSome").setParameter("filt",filter).getResultList(); + + return courseList.stream(). + map(c->new CourseDomain(c.getId(),c.getName(),c.getDescription(),c.getCourseNr())). + collect(Collectors.toList()); + } + +} diff --git a/src/main/java/Java/jpa/Course.java b/src/main/java/Java/jpa/Course.java new file mode 100644 index 0000000..a703208 --- /dev/null +++ b/src/main/java/Java/jpa/Course.java @@ -0,0 +1,63 @@ +package jpa; + +import javax.persistence.*; + +/** + * Created by Solidbeans on 2017-03-20. + */ +@Entity +@NamedQueries({ + @NamedQuery(name="selectAll",query="SELECT c FROM Course c"), + @NamedQuery(name="selectSome",query="SELECT t FROM Course t WHERE LOCATE(:filt,t.name) OR LOCATE(:filt,t.courseNr) >0 ") +}) +public class Course { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String name; + private String description; + private String courseNr; + + public Course() { + } + + public Course(String name, String description, String courseNr) { + this.name = name; + this.description = description; + this.courseNr = courseNr; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCourseNr() { + return courseNr; + } + + public void setCourseNr(String courseNr) { + this.courseNr = courseNr; + } +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index 90b915e..d67439c 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -3,17 +3,13 @@ - - - - jdbc/person + http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + + jdbc/school false - - + - - \ No newline at end of file +