Hibernate和本地化表

时间:2013-09-24 06:42:56

标签: hibernate spring-mvc localization internationalization

我有一张表,其中包含与表服务有一对多关系的酒店,该服务包含酒店提供的所有服务。这些服务必须以多种语言提供,因此对于每种服务,X语言都有X条目。

-----------             --------------------            --------------------
| Hotel   |             | Services         |            | ServicesLocale   |
-----------             |-------------------            |-------------------
| hotelId |-------------| serviceId        |------------| serviceLocaleId  |
| name    |             | category         |            | locale           |
| ...     |             | hotelId          |            | description      |
-----------             | ...              |            | serviceId        |
                        --------------------            --------------------

我正在使用Hibernate和Spring MVC在我的webapp中管理这些信息。目前我已将ServicesLocale数据映射到Services作为(String locale,ServicesLocale)的HashMap,并且在检索服务时我总是获取所有语言环境信息。但是这不能按预期工作,因为我必须列出酒店的所有服务,并按照每个特定区域的描述进行排序。

因此,最初的解决方案是仅针对特定区域设置检索服务对象,这使得hashmap映射无用(因为我只检索对象)

将特定区域设置仅映射到域对象的最佳做法是什么?

注意:我只执行只读操作,但需要通用解决方案

编辑:发布一些代码。

地图:

public class HotelVO extends BaseModel implements java.io.Serializable {


    private Long hotelId;
    private String hotel;
    private String address;
    private String code;
    private String latitude;
    private String longitude;

    private Set servicesVOs = new HashSet(0);
}

映射:

<hibernate-mapping>
    <class catalog="bdd" name="HotelVO" table="hotels">
        <id name="hotelId" type="java.lang.Long">
            <column name="hotelId" precision="4" scale="0" />
            <generator class="assigned" />
        </id>


        <property generated="never" lazy="false" name="hotel" type="java.lang.String">
            <column name="hotel" not-null="true" />
        </property>

        <property generated="never" lazy="false" name="address" type="java.lang.String">
            <column name="address" />
        </property>

        <property generated="never" lazy="false" name="code" type="java.lang.String">
            <column length="10" name="code" />
        </property>

        <property generated="never" lazy="false" name="latitude" type="java.lang.String">
            <column name="latitud" />
        </property>
        <property generated="never" lazy="false" name="longitude" type="java.lang.String">
            <column name="longitud" />
        </property>


        <set fetch="select" inverse="true" lazy="true" name="servicesVOs" sort="unsorted" table="servicios">
            <key>
                <column name="hotelId" not-null="true" />
            </key>
            <one-to-many class="ServiceVO" />
        </set>
    </class>
</hibernate-mapping>

服务:

public class ServiceVO extends BaseModel implements java.io.Serializable {

    private Long serviceId;
    private CategoryVO categoryVO;
    private HotelVO hotelVO;
    private Long coordX;
    private Long coordY;
    private Long floor;
    private Map servicesLocaleVOs = new HashMap(0);

}

映射:

<hibernate-mapping>
    <class catalog="bdd" name="ServiceVO" table="services">
        <id name="serviceId" type="int">
            <column name="serviceId" />
            <generator class="assigned" />
        </id>

        <many-to-one class="CategoryVO" fetch="join" name="categoryVO">
            <column name="categoryId" not-null="true" />
        </many-to-one>

        <many-to-one class="HotelVO" fetch="select" name="hotelVO">
            <column name="hotelId" not-null="true" />
        </many-to-one>
        <property generated="never" lazy="false" name="coordX" type="java.lang.Long">
            <column name="coord_x" precision="20" scale="0" />
        </property>
        <property generated="never" lazy="false" name="coordY" type="java.lang.Long">
            <column name="coord_y" precision="20" scale="0" />
        </property>

        <property generated="never" lazy="false" name="floor" type="java.lang.Long">
            <column name="floor" precision="3" scale="0" />
        </property>

        <map fetch="select" inverse="true" lazy="true" name="servicesLocaleVOs" sort="unsorted" table="serviceslocale">
            <key>
                <column name="serviceId" />
            </key>
            <index column="locale" type="java.lang.String" />
            <one-to-many class="LocaleServiceVO" />
        </map>
    </class>
</hibernate-mapping>

LocaleService:

public class LocaleServiceVO  extends BaseModel implements java.io.Serializable {

    private Long localeServiceId;
    private String locale;
    private ServiceVO serviceVO;
    private String name;
    private String description;
}

映射:

<hibernate-mapping>
    <class catalog="bdd" name="LocaleServiceVO" table="serviceslocale">
        <id name="localeServiceId" type="int">
            <column name="localeServiceId" />
            <generator class="assigned" />
        </id>

                <property generated="never" lazy="false" name="locale" type="java.lang.String">
            <column name="locale" />
        </property>

        <many-to-one class="ServiceVO" fetch="select" name="serviceVO">
            <column name="serviceId" />
        </many-to-one>

        <property generated="never" lazy="false" name="name" type="java.lang.String">
            <column name="name" />
        </property>

        <property generated="never" lazy="false" name="description" type="java.lang.String">
            <column length="65535" name="description" />
        </property>

    </class>
</hibernate-mapping>

0 个答案:

没有答案