Hi, I have 2 classes in bidirectional one-to-many relation. User:
Code:
@Entity
@Table(name="users")
public class User implements Serializable {
@Id
@GeneratedValue
private long id;
@Column(name="name", length=50, nullable=false)
private String name;
@Column(name="password", length=20, nullable=false)
private String password;
//fetch is eager, as it is almost sure that notes list will be needed
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="author")
@JoinColumn(name="user_id")
private List<Note> notes;
and Note:
Code:
@Entity
@Table(name="notes")
public class Note implements Serializable {
@Id
@GeneratedValue
private long id;
@Column(name="date")
@Temporal(TIMESTAMP)
private Date date;
@Column(name="content")
private String content;
@ManyToOne
@JoinColumn(name="user_id")
private User author;
How can I remove some note? I was trying this way:
Code:
session.beginTransaction();
User user = Utils.getLoggedUser();
Note note = (Note)session.get(Note.class, id);
if(user.getNotes().contains(note)) {
user.getNotes().remove(note);
session.update(user);
}
But it doesn't work!!! I was trying many ways like:
session.delete(note), and other combinations. Nothing helps so far. Even this:
Code:
@Cascade(CascadeType.DELETE_ORPHAN)
@OneToMany(cascade=javax.persistence.CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="author")
@JoinColumn(name="user_id")
private List<Note> notes;