gpt4 book ai didi

java - 使用 hbm 文件生成带有列表的表

转载 作者:行者123 更新时间:2023-11-30 03:22:48 26 4
gpt4 key购买 nike

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

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

这里是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
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

这是 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>

希望尽快得到答复。

最佳答案

我们也可以通过添加来做到这一点

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

在.cfg.xml 文件中。

关于java - 使用 hbm 文件生成带有列表的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30953203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com