I have user login info stored with JPA along with a room. The room has a list of users, the users have a transient field, which is where my problem is coming in.

public class User{

    //some other stuff ids and such

    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    public Long getId() {
        return id;

    public Room getCurrentRoom() {
        return currentRoom;

    public void setCurrentRoom(Room currentRoom) {
        this.currentRoom = currentRoom;

    private BlockingDeque<Message> getMessages() {
        return messages;

    private void setMessages(BlockingDeque<Message> messages) {
        this.messages = messages;

public class Room implements Serializable {


    private List<Player> players;

    public void sendMessage(Message m){
        Session sess = HibernateUtil.getSessionFactory().openSession();

        for(Player p : players){

    @OneToMany(mappedBy = "currentRoom",fetch = FetchType.EAGER)
    public List<Player> getPlayers(){
        return players;

    private void setPlayers(List<Player> players){


//THIS IS in the login servlet
        try {
            HttpSession httpsession = request.getSession();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            Session session = HibernateUtil.getSessionFactory().
            Query query = session.createQuery("from Player P where P.username = :username");
            query.setParameter("username", username);
            List<Player> players = (List<Player>) query.list();
                Player p2 = players.get(0);
                Player p = new Player(username, password, p2.getSalt());
                    httpsession.setAttribute("user", players.get(0));
            } else {


        } finally {

the problem here is that

User u = session.getAttribute("User");

User other = null;
for(User o : u.getCurrentRoom().getPlayers()){

//returns true because u.equals(other)
//returns false

//these are both false because messages is transient

the messaging system I want doesn't work because of this, how can I ensure that all references returned from JPA/Hibernate reference the same object if they have the same key? Or should I use a different approach?

Recommended Answers

Do you override equals for your User class and use that for all "equals" tests? That woud be the normal approach, == is never going to work all the time if objects are being re-created.

Jump to Post

All 2 Replies

Do you override equals for your User class and use that for all "equals" tests? That woud be the normal approach, == is never going to work all the time if objects are being re-created.

Yes, I do overrride equals, and the equality/non-equality isn't really the problem, the problem is that there are duplicates.

JPA/Hibernate is loading the player that logged in, then that players current room, and while loading that room, it Eagerly (temporary fix for LazyInitializationException) loads all the current players in that room.

One of those players is already in memory (the Signed on player), but when it loads the list of players it doesn't use the already existing player, but makes a new one.

This creates a problem when we access a player through his/her session or through a reference to the room, because the Player has a Transient object field (a BlockingQueue for messaging) which needs to be updated (when they send a message) through the room, to send to all players, and read (to recieve a message) through the player in the session.

Is there a way for Hibernate/JPA to cache objects (by id?) so that duplicates are not created and the same reference is returned each time? or should I just access the player like this sessionPlayer.currentRoom.getPlayer(sessionPlayer) to consistantly use the same player object?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.