add Course entity and jpa persist

PlaygroundDevelopment
nils 7 years ago
parent 7a74507b9d
commit e0cd6f12fc

@ -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;
}
}

@ -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<CourseDomain> getCourses();
public List<CourseDomain> getCoursesContaining(String filter);
}

@ -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<CourseDomain> getCourses() {
List<Course> 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<CourseDomain> getCoursesContaining(String filter) {
List<Course> 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());
}
}

@ -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;
}
}

@ -3,17 +3,13 @@
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="nameDB" transaction-type="JTA"> <!--same name as in emf definition-->
<jta-data-source>jdbc/person</jta-data-source> <!--This resource has to be added to GlassFish-->
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Persistence-unit" transaction-type="JTA">
<jta-data-source>jdbc/school</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- <property name="javax.persistence.schema-generation.database.action" value="create"/> -->
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
</persistence>

Loading…
Cancel
Save