如何使用XML实体映射(orm.xml)持久保存java.util.Properties

时间:2013-03-12 09:00:02

标签: java hibernate orm

我有一个这样的实体:

public class MyEntity implements Serializable {
  private static final long serialVersionUID = 1L;

  // some fields such as Long, String etc.

  private java.util.Properties properties;

  // getters, setters, equals, hashCode, toString ...
}

和XML实体映射

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0"
  xmlns="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">

  <persistence-unit-metadata>
    <persistence-unit-defaults>
      <access>FIELD</access>
    </persistence-unit-defaults>
  </persistence-unit-metadata>

  <entity class="com.package.of.my.MyEntity">
    <table name="my_entity">
      <unique-constraint name="uniquefield">
        <column-name>uniquefield</column-name>
      </unique-constraint>
    </table>

    <!-- some named queries -->

    <attributes>
      <id name="id"><generated-value/></id>

      <!-- some <basic name="..."> columns -->

      <!-- What to put here for mapping the properties field? -->

      <!-- other mappings (one-to-one, one-to-many, many-to-many) -->
    </attributes>
  </entity>

  <!-- other entity mappings -->
</entity-mappings>

如何理想地映射java.util.Properties对象?

使用<basic name="properties"/>会导致错误:无法解析属性。但是,它会将BLOB列添加到数据库表中。

Properties适用于String个键和值,但实际上扩展了Hashtable<Object,Object>(实现Map<Object,Object>)。这看起来像一对多。但Object不是一个实体,所以也不起作用。

0 个答案:

没有答案