2015-06-20 56 views
1

我是Hibernate的新用戶,並試圖使用.hbm映射文件在數據庫中插入ArrayList<String>。 我不想使用註釋。 我搜索瞭如何插入ArrayList<String>,並從here找到工作代碼。使用hbm文件生成帶有列表的表格

我期待含有的ArrayList<String>條目的新表將在POJO的插入可以創建一個名爲ClassTime

這裏是ClassTime.hbm.xml

<?xml version="1.0"?> 
 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
 
<!-- Generated Jun 20, 2015 2:47:36 PM by Hibernate Tools 3.4.0.CR1 --> 
 
<hibernate-mapping> 
 
    <class name="com.img.yogesh.Gym.ClassTime" table="CLASS_TIME"> 
 
     <id name="idclass_time" type="java.lang.Integer"> 
 
      <column name="IDCLASS_TIME" /> 
 
      <generator class="increment" /> 
 
     </id> 
 
     <property name="class_name" type="java.lang.String"> 
 
      <column name="CLASS_NAME" /> 
 
     </property> 
 
     <property name="repeat_boolean" type="java.lang.String"> 
 
      <column name="REPEAT_BOOLEAN" /> 
 
     </property> 
 
     <property name="repeat_weeks" type="java.lang.String"> 
 
      <column name="REPEAT_WEEKS" /> 
 
     </property> 
 
     <property name="instructor_name" type="java.lang.String"> 
 
      <column name="INSTRUCTOR_NAME" /> 
 
     </property> 
 
     <property name="start_on_date" type="java.util.Date"> 
 
      <column name="START_ON_DATE" /> 
 
     </property> 
 
     <property name="start_time_date" type="java.util.Date"> 
 
      <column name="START_TIME_DATE" /> 
 
     </property> 
 
     <property name="end_on_date" type="java.util.Date"> 
 
      <column name="END_ON_DATE" /> 
 
     </property> 
 
     <property name="end_time_date" type="java.util.Date"> 
 
      <column name="END_TIME_DATE" /> 
 
     </property> 
 
     <list name="repeat_days_list" table="repeat_days_list" > 
 
      <key > 
 
       <column name="repeat_id" /> 
 
      </key> 
 
      <list-index column="day"></list-index> 
 
      <element type="java.lang.String"> 
 
       <column name="REPEAT_DAYS_LIST" /> 
 
      </element> 
 
     </list> 
 
     <property name="repeat_days_list_string" type="java.lang.String"> 
 
      <column name="REPEAT_DAYS_LIST_STRING" /> 
 
     </property> 
 
    </class> 
 
</hibernate-mapping>

和java代碼將數據保存到數據庫中

session.persist(addClassTime); 
     transaction.commit(); 

和我的堆棧跟蹤,同時運行的代碼是

Caused by: java.sql.BatchUpdateException: Table 'gym.repeat_days_list' doesn't exist 
 
\t at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024) 
 
\t at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449) 
 
\t at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 
 
\t at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) 
 
\t ... 40 more 
 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'gym.repeat_days_list' doesn't exist 
 
\t at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
 
\t at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
 
\t at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
 
\t at java.lang.reflect.Constructor.newInstance(Unknown Source) 
 
\t at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
 
\t at com.mysql.jdbc.Util.getInstance(Util.java:386) 
 
\t at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) 
 
\t at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597) 
 
\t at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529) 
 
\t at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990) 
 
\t at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151) 
 
\t at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625) 
 
\t at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119) 
 
\t at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415) 
 
\t at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1976) 
 
\t ... 43 more

這裏是hibernate.cfg.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="dialect">org.hibernate.dialect.MySQLDialect</property> 
 
     <property name="connection.url">jdbc:mysql://localhost:3306/gym</property> 
 
     <property name="connection.username">root</property> 
 
     <property name="connection.password">root</property> 
 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
 
     
 
     <property name="hbm2ddl.auto">create</property> 
 
    <mapping resource="question.hbm.xml"/> 
 
    </session-factory> 
 

 
</hibernate-configuration>

希望很快得到答案。

回答

0

你需要告訴Hibernate創建表,如果它不存在。所以直接在ClassTime.hbm.xml加入這一行的下面<hibernate-mapping>

<property name="hbm2ddl.auto" value="create"/> 
+0

我發現應該在.cfg文件中添加而不是.hbm文件。是這樣嗎? –

+0

啊抱歉是的,這是正確的 – smoggers

+0

朋友你的建議沒有奏效。它仍然拋出相同的堆棧跟蹤 –

1

我們也可以做到這一點,在.cfg.xml文件中添加

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