在hibernate中映射集合

时间:2014-12-11 05:01:22

标签: java hibernate orm

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

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

    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>

1 个答案:

答案 0 :(得分:1)

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