LedZelkin
LedZelkin

Reputation: 586

Object persistence doesn't work

I'm trying to implement a little chat with JPA. Everythings work except one thing. I want, when my user open the connection with the endPoint, save his room number, his nickname and the timstamp. My table has been well created, columns too but i cant persist my Connexion Object in DataBase.

I'm using Glasfish 4.0 and i've already create my JDBCRessources and JDBC Connection pools who are worked well.

Here my ChatEndPoint.Java

package server;

import java.io.IOException;

import javax.websocket.EncodeException;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

    @ServerEndpoint(value = "/websocket/{room-name}/{nick-name}", encoders = { ChatMessageEncoder.class }, decoders = { ChatMessageDecoder.class })
    public class ChatEndPoint {

        // traitement de la connexion d'un client
        @OnOpen
        public void open(Session session, EndpointConfig conf, @PathParam("room-name") String roomName, @PathParam("nick-name") String nickName) throws Exception {
            System.out.println("connection ouverte");
            session.getUserProperties().put("salle", roomName);
            DAO dao=new DAO();
            dao.createConnection(nickName, roomName);

        }

        // traitement de la reception d'un message client
        @OnMessage
        public void onMessage(Session session, ChatMessage msg) throws IOException,
                EncodeException {

            if (msg instanceof ChatMessage) {

                ChatMessage reponse = new ChatMessage(msg.getEmetteur(),
                        msg.getSalle(), msg.getMessage());

                for (Session sess : session.getOpenSessions()) {
                    if (sess.isOpen()
                            && sess.getUserProperties().get("salle")
                                    .equals(msg.getSalle()))
                        sess.getBasicRemote().sendObject(reponse);
                }

            }

        }
    }

My Connexion class who represent my entity to save :

Connexion.java

package server;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;


@Entity
public class Connexion implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Column( name = "nickName" )
    protected  String nickName;

    @NotNull
    @Column( name = "roomName" )
    protected  String roomName;

    @Column( name = "dateConnexion" )
    protected  Timestamp connectionDate;


    public Long getId() {
        return id;
    }

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

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }

    public Timestamp getConnectionDate() {
        return connectionDate;
    }

    public void setConnectionDate(Timestamp timestamp) {
        this.connectionDate = timestamp;
    }
}

DAO.java

package server;

import java.sql.Timestamp;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;


@Stateless
@LocalBean
public class DAO {

    @PersistenceUnit
    private EntityManagerFactory emf;

    // Injection du manager, qui s'occupe de la connexion avec la BDD
    @PersistenceContext(unitName="projetwebee1")
    EntityManager em;



    // Enregistrement d'un nouvel utilisateur
    public void createConnection(String nickName, String roomName) throws Exception{
        this.emf=Persistence.createEntityManagerFactory("projetwebee1");
        this.em = this.emf.createEntityManager();

        Connexion c = new Connexion();
        c.setNickName(nickName);
        c.setRoomName(roomName);
        c.setConnectionDate(new Timestamp((new java.util.Date()).getTime()));
        System.out.println(" "+em);
        em.persist(c);
        System.out.println("persist OK");

    }
}

persistence.xml generated by JPA. But i edited some part of it

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="projetwebee1" transaction-type="JTA">
        <jta-data-source>JEEProjectJNDIFinal3</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="create-tables" />

        </properties>


    </persistence-unit>
</persistence>

So after i'm trying to persist my entity i cant see it in my data Explorer. I've already try a lot of other method than i found on the official doc or even here. But no one succeed.

Thanks for your help

Upvotes: 0

Views: 486

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

The EJB container injects an entity manager in your bean, but you discard it and replace it by one you create yourself. All you need is

@Stateless
@LocalBean
public class DAO {

    @PersistenceContext
    EntityManager em;

    public void createConnection(String nickName, String roomName) {
        Connexion c = new Connexion();
        c.setNickName(nickName);
        c.setRoomName(roomName);
        c.setConnectionDate(new Timestamp((new java.util.Date()).getTime()));
        em.persist(c);
    }

But the problem is that instead of letting the container create this EJB, you instanciate it by yourself, transforming what should be an injectable, transactional EJB into a dumb object, unmanaged by the EJB container. NEVER use new to get an instance of an EJB. Use dependency injection.

Instead of

@ServerEndpoint(value = "/websocket/{room-name}/{nick-name}", encoders = { ChatMessageEncoder.class }, decoders = { ChatMessageDecoder.class })
public class ChatEndPoint {

    // traitement de la connexion d'un client
    @OnOpen
    public void open(Session session, EndpointConfig conf, @PathParam("room-name") String roomName, @PathParam("nick-name") String nickName) throws Exception {
        System.out.println("connection ouverte");
        session.getUserProperties().put("salle", roomName);
        DAO dao=new DAO();
        dao.createConnection(nickName, roomName);

use

@ServerEndpoint(value = "/websocket/{room-name}/{nick-name}", encoders = { ChatMessageEncoder.class }, decoders = { ChatMessageDecoder.class })
public class ChatEndPoint {

    @Inject
    private DAO dao;

    // traitement de la connexion d'un client
    @OnOpen
    public void open(Session session, EndpointConfig conf, @PathParam("room-name") String roomName, @PathParam("nick-name") String nickName) throws Exception {
        System.out.println("connection ouverte");
        session.getUserProperties().put("salle", roomName);
        dao.createConnection(nickName, roomName);

Upvotes: 1

Related Questions