【发布时间】:2014-12-11 05:01:22
【问题描述】:
以下代码是服务层,其中包含带有一些键值对的地图。
{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();
}
}
以下是休眠配置文件
<?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>
【问题讨论】:
-
你在问如何在hibernate中映射一个地图集合吗?如果是这样 - stackoverflow.com/questions/2327971/…