使用休眠"Many to Many"时收到"Could not execute JDBC batch update"错误消息



我配置了一个"问题标签" 多对多 relationship.in 休眠当我用一个小程序测试它时,它有以下错误:(我的休眠版本是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

这是我的测试程序:公开课测试1 {

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();
}

当方法setTags(tagList(不被调用时,这个程序运行良好,但是,当我添加此方法调用时,会发生错误。(请参阅程序中的注释(。

这是 Question.hbm .xml定义的多对多属性的一部分。

    <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>

我在休眠中设置了以下属性.cfg.xml以确保表将自动更新。

<property name="hbm2ddl.auto"> update </property>

我对错误消息感到困惑,请告诉我哪里出了问题?

我在冬眠查询中找到了重点,感谢 Defresne @Andy耐心和细心。

在下面的日志中:

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 (?, ?)

"insert into qa_question_tags"的最后两行在"javaqa."目录中不存在。

所以我在 Question.hbm 中添加了以下属性 catalog="javaqa2.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>

然后问题得到解决,错误消失。

为了完成,我发布了我的 Question.hbm.xml,发现在 <class> 标签中设置的目录属性是不够的,必须在 <set> 标签中再次设置:

<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>

最新更新