使用hbm文件

时间:2015-06-20 11:00:04

标签: java hibernate arraylist

我是Hibernate的新手,并尝试使用.hbm映射文件在数据库中插入ArrayList<String>。 我不希望使用注释。 我搜索了如何插入ArrayList<String>并找到here的工作代码。

我期待在名为 ClassTime 的POJO插入时创建包含ArrayList<String>条目的新表

这里是ClassTime.hbm.xml

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

和用于将数据保存到数据库的java代码

session.persist(addClassTime); 
        transaction.commit();
运行代码时

和我的堆栈跟踪是

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

这是hibernate.cfg.xml:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

希望尽快得到答案。

2 个答案:

答案 0 :(得分:2)

我们也可以通过添加

来做到这一点
<property name="hbm2ddl.auto">update</property>

在.cfg.xml文件中。

答案 1 :(得分:0)

如果表尚不存在,您需要告诉Hibernate创建表。因此,请在<hibernate-mapping>

ClassTime.hbm.xml下方直接添加此行
<property name="hbm2ddl.auto" value="create"/>