我有很多表参考翻译表。为了区分与表或其他表相关的数据,我使用的是tableName
列。我使用自定义SQL查询很长一段时间,但现在尝试迁移到JPA / Hibernate注释。
TranslationValue实体:
public class TranslationValue implements Serializable
{
private Integer m_iKeyTranslation;
private Integer m_iKeyLanguage;
private String m_sTableName;
private String m_sFieldName;
private Integer m_iKeyRow;
private String m_sValue;
public void setKeyTranslation(Integer p_iKeyTranslation)
{
m_iKeyTranslation = p_iKeyTranslation;
}
@Id
@GeneratedValue
@Column(name = "keyTranslation")
public Integer getKeyTranslation()
{
return m_iKeyTranslation;
}
public void setKeyLanguage(Integer p_iKeyLanguage)
{
m_iKeyLanguage = p_iKeyLanguage;
}
@Column(name = "keyLanguage")
public Integer getKeyLanguage()
{
return m_iKeyLanguage;
}
public void setTableName(String p_sTableName)
{
m_sTableName = p_sTableName;
}
@Column(name = "tableName")
public String getTableName()
{
return m_sTableName;
}
public void setFieldName(String p_sFieldName)
{
m_sFieldName = p_sFieldName;
}
@Column(name = "fieldName")
public String getFieldName()
{
return m_sFieldName;
}
public void setKeyRow(Integer p_iKeyRow)
{
m_iKeyRow = p_iKeyRow;
}
@Column(name = "keyRow")
public Integer getKeyRow()
{
return m_iKeyRow;
}
public void setValue(String p_sValue)
{
m_sValue = p_sValue;
}
@Column(name = "value")
public String getValue()
{
return m_sValue;
}
}
典型参考:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name="keyRow", referencedColumnName="keyPostCustomValue"),
@JoinColumn(name="tableName", referencedColumnName="'constant'")
})
public List<TranslationValue> getValues()
{
return m_lValues;
}
public void setValues(List<TranslationValue> p_lValues)
{
m_lValues = p_lValues;
}
现在想知道是否有办法强制列tableName
在注释中有一些字符串常量(在插入/更新子节点时将设置为常量,在获取时进行过滤)。我目前收到此错误:
org.hibernate.MappingException: Unable to find column with logical name: 'constant' in common_posts_customs_values_text
如果可能,我不想使用多个实体。
答案 0 :(得分:0)
最后我稍微更改了设计并在Entity
中添加了一个需要多语言内容示例的ID:
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "keyTitle")
public MultilingualText getTitle()
{
return m_oTitle;
}
现在使用标准Entity
用于多语言文字,不再需要tableName
列来区分任何内容:
@Entity
@Table(name = "common_multilingualtexts")
public class MultilingualText implements Serializable
{
private Integer m_iKeyMultilingualText;
private Map<String, String> m_lValues = new HashMap<String, String>();
public void setKeyMultilingualText(Integer p_iKeyMultilingualText)
{
m_iKeyMultilingualText = p_iKeyMultilingualText;
}
@Id
@GeneratedValue
@Column(name = "keyMultilingualText")
public Integer getKeyMultilingualText()
{
return m_iKeyMultilingualText;
}
public void setValues(Map<String, String> p_lValues)
{
m_lValues = p_lValues;
}
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "languageCode")
@CollectionTable(name = "common_multilingualtexts_values", joinColumns = @JoinColumn(name = "keyMultilingualText"))
@Column(name = "value")
public Map<String, String> getValues()
{
return m_lValues;
}
public void put(String p_sLanguageCode, String p_sValue)
{
m_lValues.put(p_sLanguageCode,p_sValue);
}
public String get(String p_sLanguageCode)
{
if(m_lValues.containsKey(p_sLanguageCode))
{
return m_lValues.get(p_sLanguageCode);
}
return null;
}
}
它只需要两个表来实现魔术:
CREATE TABLE `common_multilingualtexts` (
`keyMultilingualText` int(11) NOT NULL auto_increment,
PRIMARY KEY (`keyMultilingualText`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
CREATE TABLE `common_multilingualtexts_values` (
`languageCode` varchar(5) NOT NULL,
`keyMultilingualText` int(11) NOT NULL,
`value` text,
PRIMARY KEY (`languageCode`,`keyMultilingualText`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
希望它会帮助别人!