I have News entity and comment entity. I want to create news with 0 comments and then add some comments to this news.
In DB I have table NEWS for news info and news_id column in COMMENTS table.
News.java:
@Entity
@Table(name = "NEWS")
public class News implements Serializable{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 883279937885116359L;
/**
* News id
*/
@Id
@GeneratedValue(generator = "seq")
@SequenceGenerator(name="seq", sequenceName="NEWS_SEQ",allocationSize=1)
@Column(name = "NEWS_ID", nullable = false, unique = true)
private Long id;
/**
* News short text
*/
@Column(name = "SHORT_TEXT")
private String shortText;
/**
* News full text
*/
@Column(name = "FULL_TEXT")
private String fullText;
/**
* News title
*/
@Column(name = "TITLE")
private String title;
/**
* News creation date
*/
@Column(name = "CREATION_DATE")
private Date creationDate;
/**
* News modification date
*/
/**
* News comments
*/
@OneToMany(cascade = CascadeType.ALL, orphanRemoval= true, fetch = FetchType.EAGER)
@JoinColumn(name = "NEWS_ID")
private List<Comment> comments;
Comment.java:
@Entity
@Table(name = "COMMENTS")
public class Comment implements Serializable{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -5697896094322498108L;
/**
* Comment id
*/
@Id
@GeneratedValue(generator = "seq")
@SequenceGenerator(name="seq", sequenceName="COMMENTS_SEQ",allocationSize=1)
@Column(name = "COMMENT_ID", nullable = false, unique = true)
private Long id;
/**
* Comment text
*/
@Column(name = "COMMENT_TEXT")
private String commentText;
/**
* Comment creation date
*/
@Column(name = "CREATION_DATE")
private Date creationDate;
/**
* Id of the news which the comment is added to
*/
@Column(name = "NEWS_ID")
private Long newsId;
I have such code:
News news = new News();
news.setTitle("qwerty");
news.setShortText("qwerty");
news.setFullText("qwerty");
jpaNewsDAO.add(news);
news = jpaNewsDAO.findById(news.getId());//everything is fine here
// 0 comments
Comment comment = new Comment(null,"qwerty",new Date(),news.getId());
jpaCommentDAO.add(comment);
news = jpaNewsDAO.findById(news.getId());
But after that news.comments have 3 items with copies(even with the same id's) of out comment and I can't understand why. There are only one such comment in DB.
Add comment:
@Override
public Long add(Comment entity) throws DAOException {
EntityManager manager = null;
EntityTransaction transaction;
try {
manager = managerFactory.createEntityManager();
transaction = manager.getTransaction();
transaction.begin();
entity.setId(null);
manager.persist(entity);
manager.flush();
transaction.commit();
} catch (PersistenceException e) {
throw new DAOException(e);
} finally {
closeManager(manager);
}
return entity.getId();
}
Add news:
@Override
public Long add(News entity) throws DAOException {
EntityManager manager = null;
EntityTransaction transaction;
try {
manager = managerFactory.createEntityManager();
transaction = manager.getTransaction();
transaction.begin();
entity.setId(null);
List<Comment> comments = new ArrayList<Comment>();
if (entity.getComments()!= null) {
for (Comment comment : entity.getComments()) {
comments.add(manager.find(Comment.class, comment.getId()));
}
}
entity.setComments(comments);
manager.persist(entity);//add
manager.flush();
transaction.commit();
} catch (PersistenceException e) {
throw new DAOException(e);
} finally {
closeManager(manager);
}
return entity.getId();
}
So why I have copies of comments?