Hi,
I have a problem with cascading creation, here is my classes :
Parent class :
@Entity
@Table(name = "users")
public class SatUser implements Serializable {
..........
private Set<Message> messages = new HashSet<Message>();
@OneToMany( cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.REMOVE
},
mappedBy = "satUser"
)
public Set<Message> getMessages() {
return messages;
}
public void setMessages(Set<Message> messages) {
this.messages = messages;
}
public void addMessage(Message message) {
message.setSatUser(this);
messages.add(message);
}
...........
}
children class :
@Entity
@Table(name="messages")
public class Message implements Serializable {
private SatUser satUser;
@ManyToOne(targetEntity = com.persistents.SatUser.class)
@JoinColumn(name = "userid", nullable = false)
public SatUser getSatUser() {
return satUser;
}
public void setSatUser(SatUser user) {
this.satUser = user;
}
}
and here I try to save new user with a message :
private void createUser() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
SatUser user = new SatUser();
user.setActivated(true);
........
user.setUserType(0);
Message message = new Message();
message.setMessage("THe first message");
message.setAttempts(0);
user.addMessage(message);
tx.begin();
Serializable ret = session.save(user);
tx.commit();
} catch (RuntimeException e) {
tx.rollback();
} finally {
//session.close();
}
}
I get no errors, no exception, and only user creates in DB, no messages, only one SQL statement generates for users table.
All code written by hibernate documentation. Does any one know where is the problem?
Thanks.
|