-->
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.  [ 4 posts ] 
Author Message
 Post subject: Cascading creation
PostPosted: Thu Sep 04, 2008 9:04 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  
 
 Post subject:
PostPosted: Fri Sep 05, 2008 5:48 am 
Newbie

Joined: Thu Sep 04, 2008 8:31 am
Posts: 5
I've changed cascade to CascadeType.ALL, now it generates both queries but I get follow error :

Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`mydb_hiber/messages`, CONSTRAINT `messages_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`id`) ON DELETE CASCADE)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 10 more


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 4:28 am 
Newbie

Joined: Thu Sep 04, 2008 8:31 am
Posts: 5
I've solved the problem, it was in primary keys generation. I've inserted @GeneratedValue before primary keys in classes and everything works fine now!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 7:57 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Yes, that should do the trick!

Sometime it may feel like you're having a conversation with yourself, but it helps everyone when you post back with your solution. Thanks!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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

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.