JPQL:如何构造WHERE子句?

时间:2012-08-08 12:00:52

标签: java jpa jpql

我想在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;

我应该在Reference&amp ;;上使用LEFT JOIN吗?用这些id选择翻译的语言?

谢谢。

1 个答案:

答案 0 :(得分: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参考和语言)并返回Translation s作为ID为refId的参考,语言为lang = lang