implementato esercizio studenti con collegamento da client via interfaccia remota
This commit is contained in:
55
exercise1/EJB.java
Normal file
55
exercise1/EJB.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package exercise1;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.ejb.LocalBean;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Stateless
|
||||
@LocalBean
|
||||
public class EJB implements RemoteInterface {
|
||||
|
||||
@PersistenceContext(unitName = "exercise1")
|
||||
EntityManager em ;
|
||||
|
||||
|
||||
public void create_student (@NotNull Student s) {
|
||||
em.persist(s);
|
||||
}
|
||||
public void update_student (@NotNull Student s) {
|
||||
em.merge(s);
|
||||
em.persist(s);
|
||||
}
|
||||
public void delete_student (@NotNull Student s) {
|
||||
em.merge(s);
|
||||
em.remove(s);
|
||||
}
|
||||
|
||||
public @NotNull List<Student> findname (@NotNull String name) {
|
||||
Query Q = em.createNamedQuery("findname");
|
||||
Q.setParameter(1, name);
|
||||
return (List<Student>)Q.getResultList();
|
||||
}
|
||||
|
||||
public @NotNull List<Student> findsurname (@NotNull String name) {
|
||||
Query Q = em.createNamedQuery("findsurname");
|
||||
Q.setParameter(1,name);
|
||||
return (List<Student>)Q.getResultList();
|
||||
}
|
||||
|
||||
public @NotNull List<Student> findyear (@NotNull String name) {
|
||||
Query Q = em.createNamedQuery("findyear");
|
||||
Q.setParameter(1,name);
|
||||
return (List<Student>)Q.getResultList();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
40
exercise1/InitializeDB.java
Normal file
40
exercise1/InitializeDB.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package exercise1;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.ejb.*;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
public class InitializeDB {
|
||||
|
||||
private Student s1,s2;
|
||||
|
||||
@Inject
|
||||
private EJB e;
|
||||
|
||||
@PostConstruct
|
||||
void pupulate (){
|
||||
s1 = new Student("mario","marra","1");
|
||||
s2 = new Student("mario","maria","2");
|
||||
e.create_student(s1);
|
||||
e.create_student(s2);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void delete () {
|
||||
e.delete_student(s1);
|
||||
e.delete_student(s2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
18
exercise1/RemoteInterface.java
Normal file
18
exercise1/RemoteInterface.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package exercise1;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
import javax.persistence.Query;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Remote
|
||||
public interface RemoteInterface {
|
||||
|
||||
@NotNull List<Student> findname (@NotNull String name);
|
||||
@NotNull List<Student> findsurname (@NotNull String name) ;
|
||||
@NotNull List<Student> findyear (@NotNull String name) ;
|
||||
|
||||
}
|
||||
52
exercise1/Student.java
Normal file
52
exercise1/Student.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package exercise1;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Entity
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "findname", query = "SELECT s FROM Student s WHERE s.name =?1"),
|
||||
@NamedQuery(name = "findsurname", query = "SELECT s FROM Student s WHERE s.surname =?1"),
|
||||
@NamedQuery(name = "findyear", query = "SELECT s FROM Student s WHERE s.years =?1")
|
||||
})
|
||||
public class Student implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
@NotNull
|
||||
private String surname;
|
||||
@NotNull
|
||||
private String years;
|
||||
|
||||
|
||||
public Student(){}
|
||||
|
||||
public Student (@NotNull String in_name, @NotNull String in_surname, @NotNull String in_year){
|
||||
|
||||
name = in_name;
|
||||
surname = in_surname;
|
||||
years = in_year;
|
||||
|
||||
}
|
||||
|
||||
public @NotNull String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public @NotNull String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public @NotNull String getYears() {
|
||||
return years;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user