-->
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 file or three insert in 1 transaction
PostPosted: Sun Feb 18, 2007 4:32 am 
Newbie

Joined: Sun Feb 18, 2007 4:06 am
Posts: 2
Please HELP!
I am using netbean 5.5 with Hibenate 3.0 and my db is mysql 1.2.9.

I have 4 tables in my project :




**************HibernateUtil.java ************************



package BEANs ;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

/**
* @netbeans.hibernate.util
*/
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);

private static SessionFactory sessionFactory;

private static SessionFactory getSessionFactory() {
try {
if (sessionFactory == null) {
Configuration configuration = new Configuration();
// load all beans
InputStream is = HibernateUtil.class.getResourceAsStream("hibernateBeans.lst");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line ;
while ((line = reader.readLine()) != null) {
configuration.addResource(line);
}
Properties properties = new Properties();
properties.load(HibernateUtil.class.getResourceAsStream("hibernate.properties"));
configuration.setProperties(properties);
sessionFactory = configuration.buildSessionFactory();
}

} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
return sessionFactory;
}

public static final ThreadLocal session = new ThreadLocal();


public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
s = getSessionFactory().openSession();
session.set(s);
}
return s;
}


public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}

}



*********************User table*************************
package BEANs ;

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

// <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 userId ">
private String userId;
/**
* @hibernate.property
*/
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
// </editor-fold>


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


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


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


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


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

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

/**
* @hibernate.set
* role="messages"
* inverse="true"
* @hibernate.collection-key
* column="USER_FK"
* @hibernate.collection-one-to-many
* class="BEANs.Message"
*/
public java.util.Set getMessages() {
return this.messages;
}

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

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

/**
* @hibernate.set
* role="message_to_Users"
* inverse="true"
* @hibernate.collection-key
* column="USER_FK"
* @hibernate.collection-one-to-many
* class="BEANs.Message_to_User"
*/
public java.util.Set getMessage_to_Users() {
return this.message_to_Users;
}

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


}


************************Message table*****************




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=" N-1 Relation to BEANs.User user ">
private BEANs.User user;

/**
* @hibernate.many-to-one
* column="USER_FK"
* class="BEANs.User"
* not-null="true"
* outer-join="auto"
*/
public BEANs.User getUser() {
return this.user;
}

public void setUser(BEANs.User user) {
this.user = user;
}
// </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"
* inverse="true"
* @hibernate.collection-key
* column="MESSAGE_FK"
* @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>


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

/**
* @hibernate.set
* role="message_to_Users"
* inverse="true"
* @hibernate.collection-key
* column="MESSAGE_FK"
* @hibernate.collection-one-to-many
* class="BEANs.Message_to_User"
*/
public java.util.Set getMessage_to_Users() {
return this.message_to_Users;
}

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

}



**************MESSAGE_TO_USER***********************


package BEANs ;
/**
*
*
* @hibernate.class
* table="MESSAGE_TO_USER"
*
*/
public class Message_to_User {

// <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: boolean deleted ">
private boolean deleted;
/**
* @hibernate.property
* type="boolean"
*/
public boolean getDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
// </editor-fold>

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

/**
* @hibernate.many-to-one
* column="USER_FK"
* class="BEANs.User"
* not-null="true"
* outer-join="auto"
*/
public BEANs.User getUser() {
return this.user;
}

public void setUser(BEANs.User user) {
this.user = user;
}
// </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>
}


******************Attach table************************

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
*/
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>
}


to insert a message with a file ,i must insert three record in db for Message ,Message_To_User, and Attach in one transaction,with this code:

public void saveMessage(Message message,String[] list,Attach attach, User user) {
Transaction tx=null;
Session session=null;
try{


session = BEANs.HibernateUtil.currentSession();
tx= session.beginTransaction();
message.setUser(user);
user.getMessages().add(message);
String id=session.save(message).toString();




//list array is list of userIds for sending message to them
for(int i=0;i<list.length;i++) {
Collection mtuList=new ArrayList();
mtu=new Message_to_User();
user=new User();
user=(User)session.load(User.class,Long.valueOf(list[i]));

mtu.setDeleted(false);


mtu.setMessage(message);
mtu.setUser(user);


message.getMessage_to_Users().add(mtu);
user.getMessage_to_Users().add(mtu);
session.save(mtu);


}


if(attach!=null){


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


session.save(attach);



}



tx.commit();
BEANs.HibernateUtil.closeSession();



} catch (HibernateException e) {
System.out.println("test ....");
e.printStackTrace();

}

but when i execute tx.commit () , an Exception occured and it is for inserting record in Attach table:

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:218)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2174)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2610)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:52)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at BEANs.MessageFacade.saveMessage(MessageFacade.java:78)
at handler.SaveMessageHandler.setFilesToDB(SaveMessageHandler.java:207)
at handler.SaveMessageHandler.processRequest(SaveMessageHandler.java:129)
at servlet.MainServlet.doPost(MainServlet.java:199)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.BatchUpdateException: Lock wait timeout exceeded; try restarting transaction
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 34 more
Hibernate: select user0_.id as id3_, user0_.userId as userId3_, user0_.password as password3_, user0_.firstName as firstName3_, user0_.lastName as lastName3_, user0_.email as email3_, user0_.deleted as deleted3_ from USER user0_ where user0_.deleted=0
Hibernate: select user0_.id as id3_, user0_.userId as userId3_, user0_.password as password3_, user0_.firstName as firstName3_, user0_.lastName as lastName3_, user0_.email as email3_, user0_.deleted as deleted3_ from USER user0_ where user0_.userId=?


Please HELP!

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.