0

我想在 JPQL 中创建一个命名查询,但我不明白它是如何工作的......

这是我的实体:

@Entity
@NamedQueries({ 
    @NamedQuery(name = "Translation.lang", 
                query = "SELECT t FROM Translation t WHERE t.reference.id = :refId AND t.language.lang = :lang")
})
@Table(name = "T_E_TRANSLATION_TRL")
public class Translation implements java.io.Serializable {

@Id
@Column(name = "TRL_ID", unique = true, nullable = false)
@TableGenerator(name="translationSeqStore", 
    table="T_S_APP_SEQ_STORE_AST", 
    pkColumnName="AST_SEQ_NAME",
    valueColumnName = "AST_SEQ_VALUE",
    pkColumnValue = "T_E_TRANSLATION_TRL.TRL_ID", 
    allocationSize=1)
@GeneratedValue(strategy=GenerationType.TABLE, 
    generator="translationSeqStore")            
private Integer id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TRL_REF_ID", nullable = false)  
private Reference reference;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TRL_LAN_ID", nullable = false)  
private Language language;

@Column(name = "TRL_LABEL", nullable = false, length = 4000)    
private String label;

我应该在参考和语言上使用 LEFT JOIN 来选择这些 id 的翻译吗?

谢谢你。

4

1 回答 1

0

我想你想要:

SELECT t FROM Translation t join fetch t.reference join fetch t.language WHERE t.reference.id = :refId AND t.language.lang = :lang

这将加入(并获取 LAZY 参考和语言)并返回Translations 用于 IDrefId和语言的参考 lang = lang

于 2012-08-08T12:10:27.807 回答