有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JPQL:多连接。如何进行命名查询?

这是我数据库的快速图表

http://img17.imageshack.us/img17/2474/mpd.png

在这个方案中,我创建了JPA实体(用于所有带有红方块的表)

我想创建一个JPQL查询,以获取所有具有引用的飞机,引用由参数给定的引用类型定义

我试过: SELECT DISTINCT a FROM Aircraft a JOIN FETCH a.references r WHERE EXISTS (SELECT ref FROM Reference ref WHERE ref = r AND ref.referenceType.id = :id)

但是我有一个错误,因为Eclipse不喜欢JOIN FETCH a.references *r*中的别名,并且请求在JUnit测试中不起作用

这里我的实体没有getter/setter:

飞机

@Entity
@Table(name = "T_R_AIRCRAFT_AIR", uniqueConstraints = @UniqueConstraint(columnNames = "AIR_NAME"))
public class Aircraft implements java.io.Serializable {

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

    @Column(name = "AIR_NAME", unique = true, nullable = false, length = 50)
    private String name;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "T_J_REF_AIR_RFA", 
               joinColumns = { @JoinColumn(name = "RFA_AIR_ID", nullable = false, updatable = false) }, 
               inverseJoinColumns = { @JoinColumn(name = "RFA_REF_ID", nullable = false, updatable = false) })
    private Set<Reference> references = new HashSet<Reference>(0);

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "T_J_MAN_AIR_MNA", 
               joinColumns = { @JoinColumn(name = "MNA_AIR_ID", nullable = false, updatable = false) }, 
               inverseJoinColumns = { @JoinColumn(name = "MNA_MAN_ID", nullable = false, updatable = false) })  
    private Set<Manual> manuals = new HashSet<Manual>(0);

    @OneToMany(fetch = FetchType.LAZY, 
               mappedBy = "aircraft",
               cascade = { CascadeType.REMOVE })
    private Set<UserConfig> userConfigs = new HashSet<UserConfig>(0);
}

**参考**

@Entity
@Table(name = "T_E_REFERENCE_REF", 
       uniqueConstraints = @UniqueConstraint(columnNames = "REF_IDENTIFIER"))
public class Reference implements java.io.Serializable {

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

    @Column(name = "REF_IDENTIFIER", unique = true, nullable = false, length = 50)  
    private String identifier;

    @Column(name = "REF_LINK")  
    private String link;

    @Column(name = "REF_OBSERVATIONS", length = 4000)
    private String observations;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "REF_RFT_ID", nullable = false)  
    private ReferenceType referenceType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "REF_MAN_ID")    
    private Manual manual;  

    @OneToMany(fetch = FetchType.LAZY, 
               mappedBy = "reference",
               cascade = { CascadeType.REMOVE })
    private Set<Translation> translations = new HashSet<Translation>(0);

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "T_J_REF_AIR_RFA", 
               joinColumns = { @JoinColumn(name = "RFA_REF_ID", nullable = false, updatable = false) },
               inverseJoinColumns = { @JoinColumn(name = "RFA_AIR_ID", nullable = false, updatable = false) })
    private Set<Aircraft> aircrafts = new HashSet<Aircraft>(0);
}

**参考类型**

@Entity
@Table(name = "T_R_REFERENCE_TYPE_RFT", 
       uniqueConstraints = @UniqueConstraint(columnNames = "RFT_TYPE"))
public class ReferenceType implements java.io.Serializable {

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

    @Column(name = "RFT_TYPE", unique = true, nullable = false, length = 50)    
    private String type;

    @OneToMany(fetch = FetchType.LAZY, 
               mappedBy = "referenceType", 
               cascade = { CascadeType.REMOVE })    
    private Set<Reference> references = new HashSet<Reference>(0);

    @OneToMany(fetch = FetchType.LAZY, 
               mappedBy = "referenceType",
               cascade = { CascadeType.REMOVE })    
    private Set<UserConfig> userConfigs = new HashSet<UserConfig>(0);
}

PS:我忘了说飞机和参考资料之间加了一张表。这是手动操作台。但我不认为有影响

PS2:JPA由Hibernate实现

知道如何进行多重连接吗? 谢谢!


共 (1) 个答案

  1. # 1 楼答案

    您可以通过以下方式更轻松地执行查询:

    SELECT DISTINCT a FROM Aircraft a JOIN FETCH a.references r 
    LEFT JOIN FETCH a.manuals 
    WHERE r.referenceType.id = :id
    

    我添加了join fetch a.manuals,否则我得到LazyInitializationException。增加了LEFT以防止缺少手册影响输出