add Course entity and jpa persist
This commit is contained in:
parent
7a74507b9d
commit
e0cd6f12fc
5 changed files with 213 additions and 9 deletions
56
src/main/java/Java/domain/CourseDomain.java
Normal file
56
src/main/java/Java/domain/CourseDomain.java
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/main/java/Java/ejb/CourseService.java
Normal file
20
src/main/java/Java/ejb/CourseService.java
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
69
src/main/java/Java/ejb/CourseServiceImpl.java
Normal file
69
src/main/java/Java/ejb/CourseServiceImpl.java
Normal file
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
63
src/main/java/Java/jpa/Course.java
Normal file
63
src/main/java/Java/jpa/Course.java
Normal file
|
|
@ -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"
|
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
|
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
|
||||||
|
<persistence-unit name="Persistence-unit" transaction-type="JTA">
|
||||||
<persistence-unit name="nameDB" transaction-type="JTA"> <!--same name as in emf definition-->
|
<jta-data-source>jdbc/school</jta-data-source>
|
||||||
|
|
||||||
<jta-data-source>jdbc/person</jta-data-source> <!--This resource has to be added to GlassFish-->
|
|
||||||
<exclude-unlisted-classes>false</exclude-unlisted-classes>
|
<exclude-unlisted-classes>false</exclude-unlisted-classes>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- <property name="javax.persistence.schema-generation.database.action" value="create"/> -->
|
<!-- <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>
|
</properties>
|
||||||
|
|
||||||
</persistence-unit>
|
</persistence-unit>
|
||||||
</persistence>
|
</persistence>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue