Cannot resolve reference Local ejb-ref - java

I have an issue in programming an EJB application. I search a solution but I still have the same problem in intelliJ with Glassfish4 :
" Cannot resolve reference [Local ejb-ref name=EJB.AdminEJB,Local 3.x interface =Interface.AdminInterface,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session] because there are [2] ejbs in the application with interface Interface.AdminInterface."
And excuse-me for my english, I'm french.
AdminInterface in a package Interface
#Local
public interface AdminInterface {
public void creerParieur(Parieur parieur);
public void supprimerParieur (String login);
public void creerBookmaker(Bookmaker bookmaker);
public void supprimerBookmaker (String login);
public void modifParieur (Parieur parieur);
public void modifBookmaker (Bookmaker bookmaker);
public void ajouterCote(Cote cote);
public void ajouterMatch (Match match);
public List<Cote> listeCote(String log);
public List<Match> listeMatch();
public List<Parieur> listeParieur();
public List<Bookmaker> listeBookmaker();
public Parieur rechercheParieur(String id);
public Bookmaker rechercheBookmaker (String id);
public void setLogin(String login);
public String getLogin();
}
AdminEJB in a package EJB
#Stateless
public class AdminEJB implements AdminInterface{
String login;
String mdp;
#PersistenceContext(unitName = "NewPersistenceUnit")
EntityManager em;
public AdminEJB(){}
public String getLogin(){
return login;
}
public void setLogin(String login){
this.login=login;
}
public String getMdp(){
return mdp;
}
public void setMdp(String mdp){
this.mdp=mdp;
}
public void creerParieur(Parieur parieur){
em.persist(parieur);
}
public void supprimerParieur(String login){
Parieur parieur=new Parieur ();
Query req=em.createQuery("select OBJECT(P) from Parieur P where P.login=:login");
req.setParameter("login", login);
parieur=(Parieur)req.getSingleResult();
em.remove(parieur);
}
public void modifParieur(Parieur parieur){
em.merge(parieur);
}
public List<Parieur> listeParieur(){
Query req=em.createQuery("select OBJECT(P) from Parieur P");
return req.getResultList();
}
public void creerBookmaker(Bookmaker bookmaker){
em.persist(bookmaker);
}
public void supprimerBookmaker(String login){
Bookmaker bookmaker;
Query req=em.createQuery("select OBJECT(B) from Bookmaker B where B.pseudo=:login");
req.setParameter("login", login);
bookmaker=(Bookmaker)req.getSingleResult();
em.remove(bookmaker);
}
public void modifBookmaker(Bookmaker bookmaker){
em.merge(bookmaker);
}
public List<Bookmaker> listeBookmaker(){
Query req=em.createQuery("select OBJECT(B) from Bookmaker B");
return req.getResultList();
}
public List<Match> listeMatch(){
Query req=em.createQuery("select OBJECT(M) from Match M");
return req.getResultList();
}
public Bookmaker rechercheBookmaker(String id){
return em.find(Bookmaker.class,id);
}
public Parieur rechercheParieur(String id){
return em.find(Parieur.class,id);
}
public void ajouterCote (Cote cote){
em.persist(cote);
}
public void ajouterMatch (Match match){
em.persist(match);
}
public List<Cote> listeCote(String log){
Bookmaker bookmaker = new Bookmaker();
bookmaker = this.rechercheBookmaker(log);
Query req = em.createQuery("select OBJECT(C) from Cote C where C.bookmaker=:bookmaker");
req.setParameter("bookmaker", bookmaker);
return req.getResultList();
}
}
ControlerBean in a package ManagedBean
#ManagedBean
#RequestScoped
public class ControlerBean implements Serializable{
Bookmaker bookmaker;
Pari pari;
Parieur parieur;
Match match;
Cote cote;
String nomObjetP;
String nomEnP;
String pseudoUser;
String pwdUser;
#EJB
private AdminInterface admin;
public ControlerBean(){
bookmaker = new Bookmaker();
parieur = new Parieur();
cote = new Cote();
match= new Match();
pari= new Pari();
}
public String getNomObjetP() {
return nomObjetP;
}
public void setNomObjetP(String nomObjetP) {
this.nomObjetP = nomObjetP;
}
public String getNomEnP() {
return nomEnP;
}
public void setNomEnP(String nomEnP) {
this.nomEnP = nomEnP;
}
public Pari getPari() {
return pari;
}
public void setPari(Pari pari){
this.pari=pari;
}
public Bookmaker getBookmaker() {
return bookmaker;
}
public void setBookmaker(Bookmaker bookmaker) {
this.bookmaker = bookmaker;
}
public Parieur getParieur() {
return parieur;
}
public void setParieur(Parieur parieur) {
this.parieur = parieur;
}
public Cote getCote() {
return cote;
}
public void setCote(Cote cote) {
this.cote = cote;
}
public Match getMatch(){
return match;
}
public void setMatch(Match match){
this.match=match;
}
public AdminInterface getAdmin() {
return admin;
}
public void setAdmin(AdminInterface admin) {
this.admin = admin;
}
public String getPseudoUser() { return pseudoUser; }
public void setPseudoUser(String pseudoUser) {
this.pseudoUser = pseudoUser;
}
public String getPwdUser() {
return pwdUser;
}
public void setPwdUser(String pwdUser) {
this.pwdUser = pwdUser;
}
public String addParieur(){
parieur.setArgent(1000);
admin.creerParieur(parieur);
return "OK";
}
public String modifParieur(){
admin.modifParieur(parieur);
return "OK";
}
public String supprParieur(){
admin.supprimerParieur(parieur.getLogin());
return "OK";
}
public String addBookmaker(){
admin.creerBookmaker(bookmaker);
return "OK";
}
public String modifBookmaker(){
admin.modifBookmaker(bookmaker);
return "OK";
}
public String supprBookmaker(){
admin.supprimerBookmaker(bookmaker.getPseudo());
return "OK";
}
public List<Bookmaker> listeBookmaker(){
return admin.listeBookmaker();
}
public List<Parieur> listeParieur(){
return admin.listeParieur();
}
public List<Match> listeMatch(){ return admin.listeMatch(); }
public String addCote(){
pseudoUser = admin.getLogin();
cote.setBookmaker(admin.rechercheBookmaker(pseudoUser));
admin.ajouterCote(cote);
return "OK";
}
public String addMatch(){
admin.ajouterMatch(getMatch());
return "OK";
}
}
Thank's very much for any help

When you have two EJBs implementing the same interface they need to be differentiated so that the container knows which one to inject.
Add the name parameter in the #Stateless annotation to all beans implementing the same interface. In the #EJB annotation, use the beanName parameter to inject the appropriate session bean implementation.
#Stateless(name="AdminEJB1")
public class AdminEJB implements AdminInterface { .... }
#EJB(beanName = "AdminEJB1")
private AdminInterface myAdminEjb;
You can also skip the name parameter in the #Stateless annotation and then use the name of the implementing class as the beanName parameter in the #EJB annotation.
#Stateless
public class AdminEJB implements AdminInterface { .... }
#EJB(beanName = "AdminEJB")
private AdminInterface myAdminEjb;

I had same error, but I didn't work with one interface for different EJBs (generated local Session Beans from entity classes). So I am putting this answer if somebody had same problem as me, as I didn't found one here. My framework generated "ejb-local-ref" tag in pom.xml on its own. After deleting it, all works perfectly.

Another cause for this problem, although uncommon, may be the delay in communicating with the JMX port. To get around this it is possible to put the key -Dhk2.parser.timeout = 300

Related

A NullPointerException is caught when I try to add a new client in a CRUD Spring boot Application

I am doing a CRUD web application using Spring boot, spring mvc and Spring Data JPA. I wanted to test my code in the main class with adding a new client. It returns me a NullPointerException. I already check my code with a debug mode. Honestly I don't see where is exactly the issue, so, if it's possible to help it will be a pleasure.
Entity class :
#Entity
#Table(name="cliente")
public class Cliente implements Serializable{
#Id #GeneratedValue
private Integer idCliente;
private String iceCliente;
private String nombreCliente;
private String apellidoCliente;
private String direccionCliente;
private String telefonoCliente;
private String emailCliente;
private TipoCliente tipoCliente;
private String cuidadCliente;
public Cliente() {
super();
}
public Cliente(String iceCliente, String nombreCliente, String apellidoCliente, String direccionCliente,
String telefonoCliente, String emailCliente, TipoCliente tipoCliente, String cuidadCliente) {
super();
this.iceCliente = iceCliente;
this.nombreCliente = nombreCliente;
this.apellidoCliente = apellidoCliente;
this.direccionCliente = direccionCliente;
this.telefonoCliente = telefonoCliente;
this.emailCliente = emailCliente;
this.tipoCliente = tipoCliente;
this.cuidadCliente = cuidadCliente;
}
public Integer getIdCliente() {
return idCliente;
}
public void setIdCliente(int idCliente) {
this.idCliente = idCliente;
}
public String getIceCliente() {
return iceCliente;
}
public void setIceCliente(String iceCliente) {
this.iceCliente = iceCliente;
}
public String getNombreCliente() {
return nombreCliente;
}
public void setNombreCliente(String nombreCliente) {
this.nombreCliente = nombreCliente;
}
public String getApellidoCliente() {
return apellidoCliente;
}
public void setApellidoCliente(String apellidoCliente) {
this.apellidoCliente = apellidoCliente;
}
public String getDireccionCliente() {
return direccionCliente;
}
public void setDireccionCliente(String direccionCliente) {
this.direccionCliente = direccionCliente;
}
public String getTelefonoCliente() {
return telefonoCliente;
}
public void setTelefonoCliente(String telefonoCliente) {
this.telefonoCliente = telefonoCliente;
}
public String getEmailCliente() {
return emailCliente;
}
public void setEmailCliente(String emailCliente) {
this.emailCliente = emailCliente;
}
public TipoCliente getTipoCliente() {
return tipoCliente;
}
public void setTipoCliente(TipoCliente tipoCliente) {
this.tipoCliente = tipoCliente;
}
public String getCuidadCliente() {
return cuidadCliente;
}
public void setCuidadCliente(String cuidadCliente) {
this.cuidadCliente = cuidadCliente;
}
ClienteService :
#Service
#Transactional
public class ClienteServiceImpl implements ClienteService {
#Autowired
ClienteRepository clienteRepository;
#Override
public Cliente agregarCliente(Cliente cliente) {
return clienteRepository.save(cliente);
}
#Override
public Cliente editarCliente(Cliente cliente) {
Optional<Cliente> clienteDB = this.clienteRepository.findById(cliente.getIdCliente());
if (clienteDB.isPresent()) {
Cliente clienteUpdate = clienteDB.get();
clienteUpdate.setIdCliente(cliente.getIdCliente());
clienteUpdate.setIceCliente(cliente.getIceCliente());
clienteUpdate.setNombreCliente(cliente.getNombreCliente());
clienteUpdate.setApellidoCliente(cliente.getApellidoCliente());
clienteUpdate.setDireccionCliente(cliente.getDireccionCliente());
clienteUpdate.setCuidadCliente(cliente.getCuidadCliente());
clienteUpdate.setTelefonoCliente(cliente.getTelefonoCliente());
clienteUpdate.setEmailCliente(cliente.getEmailCliente());
clienteRepository.save(clienteUpdate);
return clienteUpdate;
} else {
throw new RessourceNotFoundException(
"Cliente no encontrado con nombre de usuario : " + cliente.getIdCliente());
}
}
#Override
public List<Cliente> obtenerCliente() {
return this.clienteRepository.findAll();
}
#Override
public void removeCliente(Integer idCliente) {
Optional<Cliente> clienteDB = this.clienteRepository.findById(idCliente);
if (clienteDB.isPresent()) {
this.clienteRepository.delete(clienteDB.get());
} else {
throw new RessourceNotFoundException("Cliente no encontrado con nombre de usuario : " + idCliente);
}
}
#Override
public Cliente obtenerClientePorId(Integer idCliente) {
Optional<Cliente> clienteDB = this.clienteRepository.findById(idCliente);
if (clienteDB.isPresent()) {
return clienteDB.get();
} else {
throw new RessourceNotFoundException("Cliente no encontrado con nombre de usuario : " + idCliente);
}
}
ClienteRepository :
#Repository
public interface ClienteRepository extends JpaRepository<Cliente, Integer> {
}
ClienteController :
#RestController
//#RequestMapping("/index")
public class ClienteController {
#Autowired
private ClienteService clienteService;
#GetMapping("/clientes")
public ResponseEntity<List<Cliente>> obtenerCliente() {
return ResponseEntity.ok().body(clienteService.obtenerCliente());
}
#GetMapping("/clientes/{id}")
public ResponseEntity<Cliente> obtenerClientePorId(#PathVariable Integer idCliente) {
return ResponseEntity.ok().body(clienteService.obtenerClientePorId(idCliente));
}
#PostMapping("/clientes")
public ResponseEntity<Cliente> agregarCliente(#RequestBody Cliente cliente) {
return ResponseEntity.ok().body(this.clienteService.agregarCliente(cliente));
}
#PutMapping("/clientes/{id}")
public ResponseEntity<Cliente> editarCliente(#PathVariable Integer idCliente, #RequestBody Cliente cliente) {
cliente.setIdCliente(idCliente);
return ResponseEntity.ok().body(this.clienteService.editarCliente(cliente));
}
#DeleteMapping("/clientes/{id}")
public HttpStatus removeCliente(#PathVariable Integer idCliente) {
this.clienteService.removeCliente(idCliente);
return HttpStatus.OK;
}
Main class :
#SpringBootApplication
//#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class NestideasFacturasApplication {
#Autowired
public static ClienteService clienteService = new ClienteServiceImpl();
public static void main(String[] args) {
SpringApplication.run(NestideasFacturasApplication.class, args);
System.out.println("Application démarrée");
System.out.println(clienteService);
clienteService.agregarCliente(new Cliente("16565465", "Hassan", "JROUNDI", "Said Hajji", "0662165537",
"hassan.jroundi#outlook.fr", TipoCliente.EMPREZA, "Salé"));
System.out.println(clienteService);
}
Stacktrace :
Stacktrace
First, for your test scenario, it's better to use ApplicationRunner.
NestideasFacturasApplication must implement ApplicationRunner, and override run method. Then you can write your test scenario in run.
Code :
#SpringBootApplication
public class NestideasFacturasApplication implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(NestideasFacturasApplication.class, args);
}
#Override
public void run(ApplicationArguments args) {
//Your test scenario ...
}
}
Second, Change injection of ClienteService like below
#Autowired
private ClienteService clienteService;
So we have (Entire code)
#SpringBootApplication
public class NestideasFacturasApplication implements ApplicationRunner {
#Autowired
private ClienteService clienteService;
public static void main(String[] args) {
SpringApplication.run(NestideasFacturasApplication.class, args);
}
#Override
public void run(ApplicationArguments args) {
//Your test scenario
System.out.println("Application démarrée");
System.out.println(clienteService);
clienteService.agregarCliente(new Cliente("16565465", "Hassan", "JROUNDI", "Said Hajji", "0662165537", "hassan.jroundi#outlook.fr", TipoCliente.EMPREZA, "Salé"));
}
}

what is the function of formparam annotation?

I have the code below:
public class RequestBaseFormParamTO extends BaseFormParamTO {
#FormParam("channelId")
private String channelId;
#FormParam("signatureString")
private String signatureString;
public String getChannelId() {
return channelId;
}
public void setChannelId(String channelId) {
this.channelId = channelId;
}
public String getSignatureString() {
return signatureString;
}
public void setSignatureString(String signatureString) {
this.signatureString = signatureString;
}
}
what's the function of #FormParam annotation, what I know the #FormParam is used to get the value from html but in this case, the form param not place in the services. so, what's the function? I want to understand in this code. thanks
this is the other code class:
public class BaseFormParamTO {
#FormParam("transactionId")
private String transactionId;
#FormParam("transactionTime")
private String transactionTime;
public String getTransactionId() {
return transactionId;
}
public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}
public String getTransactionTime() {
return transactionTime;
}
public void setTransactionTime(String transactionTime) {
this.transactionTime = transactionTime;
}
}

JAVA Spring : Service returning a null

For some reason, my service is returning a null. The autowires are correct, the service annotation is there, the getters and setters .. But this returns a null :
public PlatformService getPlatformService() {
return platformService;
}
public void setPlatformService(PlatformService platformService) {
this.platformService = platformService;
}
on Debug, it returns platformService = null
Here is my PlatformService :
package empsuite.service;
import java.util.List;
import empsuite.model.Platform;
public interface PlatformService {
public void addPlatform(Platform platform);
public void updatePlatform(Platform platform);
public Platform getPlatformById(int id);
public List<Platform> getPlatform();
}
PlatformServiceImpl :
#Service
#Transactional
public class PlatformServiceImpl implements PlatformService {
#Autowired
PlatformDAO platformDAO;
#Transactional(readOnly = false)
public void addPlatform(Platform platform) {
getPlatformDAO().addPlatform(platform);
}
#Transactional(readOnly = false)
public void updatePlatform(Platform platform) {
getPlatformDAO().updatePlatform(platform);
}
private PlatformDAO getPlatformDAO() {
return platformDAO; }
public void setPlatformDAO(PlatformDAO platformDAO) {
this.platformDAO = platformDAO;
}
public Platform getPlatformById(int id) {
return getPlatformDAO().getPlatformById(id);
}
public List<Platform> getPlatform() {
return getPlatformDAO().getPlatform();
}
}
The DAOImpl function (with sessionfactory autowired) as it is the builder of the HQL :
public List<Platform> getPlatform() {
List list = getSessionFactory().getCurrentSession().createQuery("from Platform").list();
return list;
}
#ManagedProperty is the cause of the problem, so I overriden it and it works with this constructor :
public PlatformManagedBean() {
super();
if(platformService == null){
WebApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
platformService = ctx.getBean(PlatformService.class);
}
}

"NullPointerException: null" in play framework

I am new to play framework and I have project with java in play framework connected to the mongoDB via MorphiaPlay. My problem is that I cannot add data. This is some of my code
public class Sign extends Controller{
static Form<Group> groupForm = form(Group.class);
public static Result index() throws Exception {
// redirect to the "group Result
return redirect(routes.Sign.group());
}
public static Result group() {
return ok(views.html.sign.render(Group.all(), groupForm));
}
public static Result newGroup() {
Form<Group> filledForm = groupForm.bindFromRequest();
if(filledForm.hasErrors()) {
return badRequest(views.html.sign.render(Group.all(), filledForm));
} else {
Group.create(filledForm.get());
return redirect(routes.Sign.group());
}
}
}
#Entity
public class Group {
#Id
public ObjectId id;
#Required
public String name;
public String email;
public String username;
public String password;
public static List<Group> all() {
if (MorphiaObject.datastore != null) {
return MorphiaObject.datastore.find(Group.class).asList();
} else {
return new ArrayList<Group>();
}
}
public static void create(Group group) {
MorphiaObject.datastore.save(group);
}
And the error is
Execution exception
[NullPointerException: null]
In C:\lo\app\models\Group.java at line 37.
public static void create(Group group) {
MorphiaObject.datastore.save(group);
}
My morphiaObject class
package controllers;
public class MorphiaObject extends GlobalSettings{
static public Mongo mongo;
static public Morphia morphia;
static public Datastore datastore;
#Override
public void onStart(play.Application arg0) {
super.beforeStart(arg0);
Logger.debug("** onStart **");
try {
MorphiaObject.mongo = new Mongo("127.0.0.1", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
}
MorphiaObject.morphia = new Morphia();
MorphiaObject.datastore = MorphiaObject.morphia.createDatastore(MorphiaObject.mongo, "project");
MorphiaObject.datastore.ensureIndexes();
MorphiaObject.datastore.ensureCaps();
Logger.debug("** Morphia datastore: " + MorphiaObject.datastore.getDB());
}
}
Can you please help!

XStream, CircularReferenceException

Please consider this code. Is it using Circular Reference? If not why am I getting CircularReferenceException, while enabling NO_REFERENCE mode in XStream. Anyone, please clarify the thing.
#XStreamAlias("BalanceEnquiry")
public class BalanceEnquiry extends EAIRequest {
#XStreamImplicit
private List<BalanceEnquiry.Detail> details;
public List<Detail> getDetails() {
....
}
public void setDetails(Detail... details) {
....
}
#XStreamAlias("details")
public final class Detail {
#XStreamAsAttribute
private String item;
private BalanceEnquiry.Detail.Request request;
public String getItem() {
....
}
public void setItem(String item) {
....
}
public Request getRequest() {
....
}
public void setRequest(Request request) {
....
}
public final class Request {
private String code;
private String branch;
public String getCode() {
....
}
public void setCode(String code) {
....
}
public String getBranch() {
....
}
public void setBranch(String branch) {
....
}
}
}
}
I suspect it's because Detail is an inner class. As such, it has an implicit reference to the instance of the outer class (and hence forms a circular reference). See here for more details.

Categories