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.
@Entity
public class User{
//some other stuff ids and such
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public Long getId() {
return id;
}
@ManyToOne
public Room getCurrentRoom() {
return currentRoom;
}
public void setCurrentRoom(Room currentRoom) {
this.currentRoom = currentRoom;
}
@Transient
private BlockingDeque<Message> getMessages() {
return messages;
}
private void setMessages(BlockingDeque<Message> messages) {
this.messages = messages;
}
}
@Entity
public class Room implements Serializable {
//OTHER STUFF
private List<Player> players;
public void sendMessage(Message m){
Session sess = HibernateUtil.getSessionFactory().openSession();
for(Player p : players){
p.addMessage(m);
}
sess.close();
}
@OneToMany(mappedBy = "currentRoom",fetch = FetchType.EAGER)
public List<Player> getPlayers(){
return players;
}
private void setPlayers(List<Player> players){
this.players=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().
openSession();
session.beginTransaction();
Query query = session.createQuery("from Player P where P.username = :username");
query.setParameter("username", username);
List<Player> players = (List<Player>) query.list();
if(players.size()==1){
Player p2 = players.get(0);
Player p = new Player(username, password, p2.getSalt());
if(p.getPassword().equals(p2.getPassword())){
httpsession.setAttribute("user", players.get(0));
//USES THE ONE RETURNED FROM HIBERNATE
}
} else {
}
session.getTransaction().commit();
response.sendRedirect("./index.jsp");
session.close();
} finally {
out.close();
}
the problem here is that
User u = session.getAttribute("User");
User other = null;
for(User o : u.getCurrentRoom().getPlayers()){
if(o.equals(u)){
other=o;
break;
}
}
u.getCurrentRoom().getPlayers().contains(u)
//returns true because u.equals(other)
u==o
//returns false
u.messages==o.messages
u.messages.equals(o.messages)
//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?