Olá pessoal, estou tentando criar um projeto EAR teste com EJB + Glassfish porém meu EntityManager vem sempre null e não sei mais o que pode ser… abaixo segue a estrutura e os códigos(bem simples) apenas para teste
EAR
|EA Client
|__ Main.java
|_________ NewClass.java
|_______EJB Module
|_________PersonSessionBean.java
|_________PersonSessionBeanLocal.java
|_________Person.java
Main.java
public class Main {
public static void main(String[] args) {
NewClass newclass = new NewClass();
List<Person> listP = new LinkedList<Person>();
for (Person person : newclass.listPeople()) {
System.out.println("People " + person.getName());
}
}
}
NewClass.java
@Named
public class NewClass {
@Inject
public PersonSessionBeanLocal personSessionBeanLocal;
public List<Person> listPeople(){
personSessionBeanLocal = new PersonSessionBean();
return personSessionBeanLocal.getPeople();
}
}
PersonSessionBean.java
@Stateless
public class PersonSessionBean implements PersonSessionBeanLocal {
@PersistenceContext
public EntityManager em;
public EntityManager getEm() {
return em;
}
@Override
public List<Person> getPeople() {
List<Person> list = new LinkedList<Person>();
list = getList(Person.class, "Select p From Person p");
return list;
}
public <T> List<T> getList(Class<T> classToCast, String query, Object... values){
Query qr = createQuery(query, values);
return qr.getResultList();
}
private Query createQuery(String query, Object[] values) {
Query qr = em.createQuery(query);
if(values != null){
for (int i = 0; i < values.length; i++) {
Object object = values[i];
qr.setParameter(i+1, object);
}
}
return qr;
}
}
PersonSessionBeanLocal.java
@Local
public interface PersonSessionBeanLocal {
public List<Person> getPeople();
}
Person.java
@Entity
@Table(name = "person")
@XmlRootElement
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Long id;
@Size(max = 255)
@Column(name = "name")
private String name;
public Person() {
}
public Person(Long id) {
this.id = id;
}
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;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Person)) {
return false;
}
Person other = (Person) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entities.Person[ id=" + id + " ]";
}
}
Obrigado!