-->
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: problem to insert record with one-to-many association
PostPosted: Tue Feb 20, 2007 7:54 am 
Newbie

Joined: Sun Feb 18, 2007 4:06 am
Posts: 2
Hi!
i work with:nebeans 5.5 , hibernate 3.0 and mysql
i have two tables with one-to-many association : Message , Attach


POJO classes :

///////////////////////Message.java


package BEANs ;

import java.util.HashSet;
/**
*
*
* @hibernate.class
* table="MESSAGE"
*
*/
public class Message {

// <editor-fold defaultstate="collapsed" desc=" PrimaryKey: long id ">
private long id;
/**
* @hibernate.id
* generator-class="increment"
* type="long"
*/
public long getId () {
return id;
}
public void setId (long id) {
this.id = id;
}
//</editor-fold>


// <editor-fold defaultstate="collapsed" desc=" Property: String content ">
private String content;
/**
* @hibernate.property
*/
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc=" Property: String subject ">
private String subject;
/**
* @hibernate.property
*/
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
// </editor-fold>


// <editor-fold defaultstate="collapsed" desc=" Property: String date ">
private String date;
/**
* @hibernate.property
*/
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
// </editor-fold>


// <editor-fold defaultstate="collapsed" desc=" Property: boolean viewStatus ">
private boolean viewStatus;
/**
* @hibernate.property
* type="boolean"
*/
public boolean getViewStatus() {
return viewStatus;
}
public void setViewStatus(boolean viewStatus) {
this.viewStatus = viewStatus;
}
// </editor-fold>



// <editor-fold defaultstate="collapsed" desc=" 1-N Relation to Collection /*BEANs.Attach*/ attachs ">
private java.util.Set attachs=new HashSet();

/**
* @hibernate.set
* role="attachs"
* @hibernate.collection-key
* column="MESSAGE_FK"
* not-null="true"
* @hibernate.collection-one-to-many
* class="BEANs.Attach"
*/
public java.util.Set getAttachs() {
return this.attachs;
}

public void setAttachs(java.util.Set attachs) {
this.attachs = attachs;
}
// </editor-fold>

}






///////////Attach.java


package BEANs ;

/**
*
*
* @hibernate.class
* table="ATTACH"
*
*/
public class Attach {

// <editor-fold defaultstate="collapsed" desc=" PrimaryKey: long id ">
private long id;
/**
* @hibernate.id
* generator-class="increment"
* type="long"
*/
public long getId () {
return id;
}
public void setId (long id) {
this.id = id;
}
//</editor-fold>




// <editor-fold defaultstate="collapsed" desc=" Property: java.sql.Blob file ">
private java.sql.Blob file;
/**
* @hibernate.property
* type="blob"
*/
public java.sql.Blob getFile() {
return file;
}
public void setFile(java.sql.Blob file) {
this.file = file;
}
// </editor-fold>



// <editor-fold defaultstate="collapsed" desc=" Property: String fileName ">
private String fileName;
/**
* @hibernate.property
*/
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
// </editor-fold>


// <editor-fold defaultstate="collapsed" desc=" Property: String type ">
private String type;
/**
* @hibernate.property
*/
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
// </editor-fold>



// <editor-fold defaultstate="collapsed" desc=" N-1 Relation to BEANs.Message message ">
private BEANs.Message message;

/**
* @hibernate.many-to-one
* column="MESSAGE_FK"
* class="BEANs.Message"
* not-null="true"
* outer-join="auto"
*/
public BEANs.Message getMessage() {
return this.message;
}

public void setMessage(BEANs.Message message) {
this.message = message;
}
// </editor-fold>
}




I want to insert record in Attach by this code:


sessionH = BEANs.HibernateUtil.currentSession();
tx= sessionH.beginTransaction();

message=new Message();
message=(Message)sessionH.load(Message.class,Long.valueOf(id));

message.getAttachs().add(attach);
attach.setMessage(message);


sessionH.save(attach);
tx.commit();
BEANs.HibernateUtil.closeSession();



an exception :
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update



but when i clear the one-to-many association between two tables , i can insert record in Attach successfully , so i think my association is not correct !

can any body help me! please....

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.