我有一个包含酒店的表,它与包含酒店提供的所有服务的表服务具有一对多的关系。这些服务必须以多种语言提供,因此对于每个服务,将有 X 种语言的 X 个条目。
----------- -------------------- --------------------
| Hotel | | Services | | ServicesLocale |
----------- |------------------- |-------------------
| hotelId |-------------| serviceId |------------| serviceLocaleId |
| name | | category | | locale |
| ... | | hotelId | | description |
----------- | ... | | serviceId |
-------------------- --------------------
我正在使用 Hibernate 和 Spring MVC 在我的 web 应用程序中管理这些信息。目前,我已将 ServicesLocale 数据作为(字符串语言环境,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>
区域服务:
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>