-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Cascading creation
PostPosted: Thu Sep 04, 2008 9:01 am 
Newbie

Joined: Thu Sep 04, 2008 8:31 am
Posts: 5
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.