学习Hibernate的过程中,遇到了表的一对多问题(一个用户可以有多个Email),向大侠们求教了
出现错误:
2875 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 208, SQLState: S0002
2875 [main] ERROR org.hibernate.util.JDBCExceptionReporter - 对象名  'demail' 无效。
2875 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: could not insert collection: [com.zwb.example.Admin.email#6]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1233)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1031)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:369)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.zwb.example.Test.add(Test.java:27)
at com.zwb.example.Test.main(Test.java:11)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名  'demail' 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1196)
... 11 more
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert collection: [com.zwb.example.Admin.email#6]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1233)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1031)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:369)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.zwb.example.Test.add(Test.java:27)
at com.zwb.example.Test.main(Test.java:11)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名  'demail' 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1196)
... 11 more
Admin.hbm.xml配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.zwb.example.Admin" table="admin" schema="dbo" catalog="blog">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity"></generator>
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="200" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="200" />
        </property>
        <set name="email" table="demail">
        <key column="userid" foreign-key="id"></key>
        <element type="java.lang.String" column="email"></element>
        </set>
    </class>
</hibernate-mapping>hibernate.config.xml配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration> <session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;DatabaseName=blog
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="myeclipse.connection.profile">mssql</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>
<mapping resource="com/zwb/example/Admin.hbm.xml" />
</session-factory></hibernate-configuration>
POJO类
package com.zwb.example;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
//设计模式,单子模式
public class HibernateSessionFactoryUtil {
  private static SessionFactory sessionFactory;
 //定义一个私有的构造方法,其他的将不能调用它的构造方法来创建一个对象,只能通过下面的静态方式调用(HibernateSessionFactoryUtil.getSessionFactory())
  private HibernateSessionFactoryUtil(){
  
  }
static {
  sessionFactory=new Configuration().configure().buildSessionFactory(); }
public static SessionFactory  getSessionFactory(){
  return sessionFactory;
}
}
package com.zwb.example;import java.util.Set;import org.hibernate.Session;public class Test { public static void main(String[] args) {
Test test=new Test();
test.add();
} public void add(){
Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
//session.beginTransaction();
Admin admin=new Admin();
admin.setUsername("zhengweibing");
admin.setPassword("55555555");
//admin.getEmail().add("sdfsafsd");
Set email=admin.getEmail();
email.add("[email protected]");
email.add("[email protected]");
email.add("[email protected]");
    session.beginTransaction();
session.save(admin);
session.getTransaction().commit();

}

解决方案 »

  1.   

    Java高级群:45271133 能容纳500人,有菜鸟也有高手,大家多多交流~!
      

  2.   

          <set name="email" table="demail"> 
            <key column="userid" foreign-key="id"> </key> 
            <element type="java.lang.String" column="email"> </element> 
          </set>  这部分配置不对 ,没有指明一对多 对应关系。改为:
          <set name="email" table="demail"> 
            <key column="userid" foreign-key="id"> </key> 
              <one-to-many class="你的demail对应的entity完整类名" />        <element type="java.lang.String" column="email"> </element> 
          </set>