I'm learning jpa and decided to do a basic webapp where a User can post Messages, so a user have many messages. The problem is that when I add a message to the User's messages collection, I get duplicated entries in the Message table. (note I have two tables with the owner Id on Message, there is no join table).
This is the conversation:
First message -> Larry says: Hello John
Second message -> Larry says: Are you there?
Third Message -> John says: Yep
Fourth Message -> Larry says: ok
Fifth Message -> John says: fine
This is what I get in the screen:
Larry: Hello John
Larry Hello John
Larry: Are you there?
John: Yep
Larry: Hello John
Larry: Are you there?
Larry: ok
John: Yep
John: fine
The table content is:
+----+----------------+----------+
| id | message | owner_id |
+----+----------------+----------+
| 1 | Hello John | NULL | <- Why the NULL fields?
| 2 | Hello John | NULL |
| 3 | Are you there? | NULL |
| 4 | Yep | NULL |
| 5 | Hello John | 1 |
| 6 | Are you there? | 1 |
| 7 | ok | 1 |
| 8 | Yep | 2 |
| 9 | fine | 2 |
+----+----------------+----------+
These are the classes I have and its mappings.
@Entity
public class User implements Serializable {
@Id
@GeneratedValue
private Long id;
private String username;
private String password;
@OneToMany (fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="owner_id")
private List<Message> messages; ...and all of its corresponding getters and setters
@Entity
public class Message implements Serializable {
@Id
@GeneratedValue
private Long id;
private String message;
@ManyToOne
private User owner; //...and all of its corresponding getters and setters
This is the relevant jsp scriptlet section which saves the user:
User user = (User)session.getAttribute("user");
user.getMessages().add(new Message(request.getParameter("message")));
FactoryDAO.getInstance().getUserDAO().update(user);
update(user) method:
public void update(User user) {
initEntityManager(); <- method in the superclass which initializes the entityManager
tx.begin();
entityManager.merge(user);
entityManager.flush();
tx.commit();
closeEntityManager();
}
Can you help me to find out what is wrong here? Thanks!