4
votes

I have a problem with my first Java EE aplication. I use Wildfly 8.1 server and Netbeans.
My application consists of Entity class (SocialUser.java). :

package com.mycompany.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "soc_user")
public class SocialUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String login;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@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 SocialUser)) {
        return false;
    }
    SocialUser other = (SocialUser) 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 "com.mycompany.entity.NewEntity[ id=" + id + " ]";
}

/**
 * @return the login
 */
public String getLogin() {
    return login;
}

/**
 * @param login the login to set
 */
public void setLogin(String login) {
    this.login = login;
}

}

Contrroller class (UserMB.java):

package com.mycompany;

import com.mycompany.service.CRUD;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;


@Named
@RequestScoped
public class UserMB {
@Inject 
private CRUD crud;

private String login;

public UserMB() {

}
public String getLogin() {
    return login;
}

public void setLogin(String login) {
    this.login = login;
}

public void save(){
    crud.businessMethod();
}

}  

and CRUD.java class which do business logic:

package com.mycompany.service;

import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@Stateless
@LocalBean
public class CRUD {

@PersistenceContext(unitName = "SocialPU")
private EntityManager em;

public void businessMethod() {
    em.persist(this);
    System.out.println("zapisano");//this is for checking
}

}

and i have this error:

Request
{
"address" => [("deployment" => "Praktyki-SocialPage-ear-1.0-SNAPSHOT.ear")],
"operation" => "deploy"
}

Response

Internal Server Error
{
"outcome" => "failed",
"failure-description" => {"JBAS014671: Failed services" =>      {"jboss.deployment.unit.\"Praktyki-SocialPage-ear-1.0-SNAPSHOT.ear\".WeldStartService" =>   "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"Praktyki- SocialPage-ear-1.0-SNAPSHOT.ear\".WeldStartService: Failed to start service
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied  dependencies for type CRUD with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private com.mycompany.UserMB.crud
at com.mycompany.UserMB.crud(UserMB.java:0)
"}},
"rolled-back" => true
}

Code seems to be ok. I also have beans.xml file in META-INF folder. I try to find answer, but i can't find any that matches my case. Can you explain my why there's a problem?

1

1 Answers

0
votes

Your class CRUD is an EJB whereas you try to inject it using CDI (@Inject in a EE container uses CDI). Alter your code to:

Contrroller class (UserMB.java):

@Named
@RequestScoped
public class UserMB {

@EJB 
private CRUD crud;