我在许多项目中使用 Hibernate。从那时起,我就开发了我的项目,包括一个翻译表,其中包含每种语言的文本值,供其他需要它的表使用。
转换表的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class dynamic-insert="false" dynamic-update="false" mutable="true" name="com.spectotechnologies.website.common.helper.TranslationValue" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="common_translations">
<id name="keyTranslation">
<generator class="native"/>
</id>
<property name="keyLanguage"/>
<property name="tableName"/>
<property name="fieldName"/>
<property name="keyRow"/>
<property name="value"/>
</class>
</hibernate-mapping>
一个需要翻译的对象的例子:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class dynamic-insert="false" dynamic-update="false" mutable="true" name="com.spectotechnologies.website.common.helper.Category" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="common_categories">
<id name="keyCategory">
<generator class="native"/>
</id>
<property name="keyParent"/>
<property name="tag"/>
<property name="name" insert="false" update="false"/>
<property name="description" insert="false" update="false"/>
</class>
</hibernate-mapping>
目前,我使用这种 SQL 查询来加载对象
"SELECT" +
" c.keyCategory," +
" c.keyParent," +
" c.tag," +
" tn.value AS name," +
" td.value AS description" +
" FROM common_categories c" +
// name
" LEFT JOIN common_translations tn" +
" ON tn.tableName = 'common_categories'" +
" AND tn.fieldName = 'name'" +
" AND tn.keyRow = c.keyCategory" +
" AND tn.keyLanguage = ?" +
// description
" LEFT JOIN common_translations td" +
" ON td.tableName = 'common_categories'" +
" AND td.fieldName = 'description'" +
" AND td.keyRow = c.keyCategory" +
" AND td.keyLanguage = ?" +
" ORDER BY" +
" c.keyCategory ASC"
我想知道是否可以在映射中传输这种左连接,因为我需要包含参数:tableName、fieldName、语言。
非常感谢您的帮助,我将能够简化很多查询!
编辑 :
这是我期望的一个例子:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class dynamic-insert="false" dynamic-update="false" mutable="true" name="com.spectotechnologies.website.common.helper.Category" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="common_categories">
<id name="keyCategory">
<generator class="native"/>
</id>
<property name="keyParent"/>
<property name="tag"/>
<one-to-many class="com.spectotechnologies.website.common.helper.TranslationValue"
formula="tableName = 'common_categories' AND fieldName = 'name' AND keyLanguage = @language AND keyRow = keyCategory" name="name" />
<one-to-many class="com.spectotechnologies.website.common.helper.TranslationValue"
formula="tableName = 'common_categories' AND fieldName = 'description' AND keyLanguage = @language AND keyRow = keyCategory" name="description" />
</class>
</hibernate-mapping>
在这种情况下,我需要创建一个可以在查询时注入的变量@language。这个解决方案有两个问题:
我不知道是否可以将变量发布到映射范围。