Please help, I'm stuck for several days. Read lots of tutorials, forums, and no working answer found.
The problem is a simple situation: many-to-many with jointable and additional fields in the jointable.
HibernateTools generate code for me like below. THE PROBLEM IS: I cannot add anything to the jointable
I have three tables:
* note
* user
* participation (which is jointable with additional field)
The generated code is:
Code:
// Generated 2011-06-04 00:33:14 by Hibernate Tools 3.4.0.CR1
// imports ...
@Entity
@Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class User implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer userId;
private String email;
private Set<Participation> participations = new HashSet<Participation>(0);
// other properties
public User() {
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "userId", unique = true, nullable = false)
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
@Column(name = "email", unique = true, nullable = false)
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<Participation> getParticipations() {
return this.participations;
}
public void setParticipations(Set<Participation> participations) {
this.participations = participations;
}
// other setters and getters
}
Code:
// Generated 2011-05-31 00:27:54 by Hibernate Tools 3.4.0.CR1
// imports...
@Table(name = "note")
public class Note implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer noteId;
private String title;
private Set<Participation> participations = new HashSet<Participation>(0);
public Note() {
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "noteId", unique = true, nullable = false)
public Integer getNoteId() {
return this.noteId;
}
public void setNoteId(Integer noteId) {
this.noteId = noteId;
}
@Column(name = "title", nullable = false, length = 65535)
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "note")
public Set<Participation> getParticipations() {
return this.participations;
}
public void setParticipations(Set<Participation> participations) {
this.participations = participations;
}
}
Code:
// Generated 2011-06-04 00:33:14 by Hibernate Tools 3.4.0.CR1
// imports...
@Entity
@Table(name = "participation")
public class Participation implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ParticipationId id;
private Note note;
private User user;
private Boolean accepted;
public Participation() {
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "noteId", column = @Column(name = "noteId", nullable = false)),
@AttributeOverride(name = "userId", column = @Column(name = "userId", nullable = false)) })
public ParticipationId getId() {
return this.id;
}
public void setId(ParticipationId id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "noteId", nullable = false, insertable = false, updatable = false)
public Note getNote() {
return this.note;
}
public void setNote(Note note) {
this.note = note;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false, insertable = false, updatable = false)
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
@Column(name = "accepted")
public Boolean getAccepted() {
return this.accepted;
}
public void setAccepted(Boolean accepted) {
this.accepted = accepted;
}
}
Code:
// Generated 2011-06-04 00:33:14 by Hibernate Tools 3.4.0.CR1
// imports ...
@Embeddable
public class ParticipationId implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int noteId;
private int userId;
public ParticipationId() {
}
public ParticipationId(int noteId, int userId) {
this.noteId = noteId;
this.userId = userId;
}
@Column(name = "noteId", nullable = false)
public int getNoteId() {
return this.noteId;
}
public void setNoteId(int noteId) {
this.noteId = noteId;
}
@Column(name = "userId", nullable = false)
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof ParticipationId))
return false;
ParticipationId castOther = (ParticipationId) other;
return (this.getNoteId() == castOther.getNoteId())
&& (this.getUserId() == castOther.getUserId());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getNoteId();
result = 37 * result + this.getUserId();
return result;
}
}
So as mentioned THE PROBLEM IS: I cannot add anything to jointable "participation"
I try this:
Code:
Note note = ... //get persistent note
User user = ... //get persistent user
Participation participation = new Participation();
ParticipationId participationId = new ParticipationId(note.getNoteId(), user.getUserId());
participation.setId(participationId);
participation.setNote(note);
participation.setUser(user);
participation.setAccepted(true);
note.getParticipations().add(participation);
user.getParticipations().add(participation);
participationDAO.createParticipation(participation);
noteDAO.updateOrSaveNote(note);
userDAO.updateOrSaveUser(user);
PLEASE, tell me what's wrong?
.