gpt4 book ai didi

java - 在 hibernate 中映射集合

转载 作者:行者123 更新时间:2023-12-01 12:08:21 25 4
gpt4 key购买 nike

以下代码是服务层,其中包含带有一些键值对的 map 。

{1=A、2=B、3=c、4=D}我想使用 hibernate 将其存储在 oracle 数据库中。我之前使用模型类映射执行此操作,但我想将此映射到此集合。

    public class CollectionMapping {

public static void main(String[] args) {

LinkedHashMap map = new LinkedHashMap();
map.put(1, "A");
map.put(2, "B");
map.put(3, "c");
map.put(4, "D");
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(map);
session.getTransaction().commit();
}

}

以下是 hibernate 配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<property name="connection.driver_class">Oracle Driver</property>
<property name="connection.url">URL</property>
<property name="connection.username">UserName</property>
<property name="connection.password">PassWord</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

这个映射类用于模型类,但如何为上述集合进行映射

    //  <mapping class="org.symp.dto.UserDetails"/>

</session-factory>
</hibernate-configuration>

最佳答案

我不确定您尝试映射到的数据库架构是什么样的,但这应该有所帮助: https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/collections.html

关于java - 在 hibernate 中映射集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27415661/

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