'I get an error message "Could not execute JDBC batch update" when using hibernate "Many to Many"
I configured a "question-tag" Many-to-Many relationship.in Hibernate When I test it with a small program, it has the follwing error: (My Hibernate version is 3.1)
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into javaqa2.qa_tag (creator_id, name, description, create_date, used_cnt) values (?, ?, ?, ?, ?)
Hibernate: insert into javaqa2.qa_tag (creator_id, name, description, create_date, used_cnt) values (?, ?, ?, ?, ?)
Hibernate: insert into javaqa2.qa_question (creator_id, subject, content, creator_ip, question_tags, upvote_cnt, downvote_cnt, visit_cnt, answer_cnt, comment_cnt, istopmost, islocked, isanonym, create_date) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into qa_question_tags (question_id, tag_id) values (?, ?)
Hibernate: insert into qa_question_tags (question_id, tag_id) values (?, ?)
Exception in thread "main" 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:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at test.Test1.main(Test1.java:49)
Caused by: java.sql.BatchUpdateException: No database selected
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 8 more
This is my Test Program: public class Test1 {
public static void main(String[] args) {
Configuration cfg=new Configuration().configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session = sf.openSession();
Transaction trans = session.beginTransaction();
Student student = (Student)session.load(Student.class, Integer.valueOf(45));
Question question=new Question(student, "test8881","test8882",
"192.168,88.88","Java,dotNet,Struts2", 0,0,0,0,0,
Boolean.FALSE, Boolean.FALSE, Boolean.FALSE,
new Date());
Tag tag1=new Tag(student,"test tag1","test tag1",new Date(),0);
Tag tag2=new Tag(student,"test tag2","test tag2",new Date(),0);
session.save(tag1);
session.save(tag2);
Set<Tag> tagList =new HashSet<Tag>();
tagList.add(tag1);
tagList.add(tag2);
question.setTags(tagList); // when add this line... error occurs
session.save(question);
trans.commit();
}
This program runs well when the method setTags(tagList) is not called for question, however, when I add this method call, the error occurs. (see the comment in the program).
This is the portion of Question.hbm.xml defined Many-to-Many properties.
<set name="tags" table="qa_question_tags" lazy="true" cascade="all">
<key column="question_id"/>
<many-to-many class="model.Tag" column="tag_id"/>
</set>
I have set the following property in my hibernate.cfg.xml to ensure the table will be updated automatically.
<property name="hbm2ddl.auto"> update </property>
I was confused with the error message, and please tell me where is the wrong?
Solution 1:[1]
I have found the point in my hibernate queries, thanks for the patience and carefulness of @Andy Defresne .
In the log below:
Hibernate: insert into javaqa2.qa_tag (creator_id, name, description, create_date, used_cnt) values (?, ?, ?, ?, ?)
Hibernate: insert into javaqa2.qa_tag (creator_id, name, description, create_date, used_cnt) values (?, ?, ?, ?, ?)
Hibernate: insert into javaqa2.qa_question (creator_id, subject, content, creator_ip, question_tags, upvote_cnt, downvote_cnt, visit_cnt, answer_cnt, comment_cnt, istopmost, islocked, isanonym, create_date) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into qa_question_tags (question_id, tag_id) values (?, ?)
Hibernate: insert into qa_question_tags (question_id, tag_id) values (?, ?)
The last two lines of "insert into qa_question_tags" are absent of "javaqa." catalog.
So I add the following property catalog="javaqa2" in the Question.hbm.xml :
<set name="tags" table="qa_question_tags" lazy="true" cascade="all" catalog="javaqa2">
<key column="question_id"/>
<many-to-many class="model.Tag" column="tag_id"/>
</set>
Then the problem is solved and the error disappears.
To be complete , I post my Question.hbm.xml, and found catalog properties set in the <class> tag is not enough, it must be set in the <set> tag again:
<hibernate-mapping>
<class name="model.Question" table="qa_question" catalog="javaqa2">
...
<set name="tags" table="qa_question_tags" lazy="true" cascade="all" catalog="javaqa2">
<key column="question_id"/>
<many-to-many class="model.Tag" column="tag_id"/>
</set>
</class>
</hibernate-mapping>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
