我在使用Hibernate“多对多”时收到错误信息“无法执行JDBC批处理更新”,

sy5wg1nm  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(127)

我配置了一个“问号”多对多关系。在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();
}

当方法setTgs(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>

我搞错了错误信息,请告诉我哪里错了?

jei2mxaa

jei2mxaa1#

我已经在我的休眠查询中找到了要点,感谢@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_QUEST_TAG”的最后两行没有“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>

相关问题