使用 hibernate“多对多”时收到错误消息“无法执行 JDBC 批量更新”

问题描述 投票:0回答:1

我在 Hibernate 中配置了“问题标签”多对多关系 当我用一个小程序测试它时,出现以下错误: (我的Hibernate版本是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>

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

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

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

java hibernate many-to-many
1个回答
0
投票

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

在下面的日志中:

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.xml 中添加以下属性 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>

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

为了完整,我发布了我的 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>
© www.soinside.com 2019 - 2024. All rights reserved.