具有@JoinColumns关系的JPQL的问题

时间:2016-09-01 17:57:57

标签: java jpa jpql

在我的项目中,我有两个实体,其中一个有EmbeddedId,因为有复合键。我想要创建的是一个可以通过列表过滤的查询。问题是列表有一个复合键,当我执行 NamedQuery 时,不会发生异常。然后我想知道我做错了什么或我的关系映射不正确。我粘贴了部分代码供您使用。

CostType.class

@Entity
@Table(name = "COST_TYPE")
@XmlRootElement
@NamedQuery(name = "CostType.findAll", query = "SELECT c FROM CostType c")
public class CostType implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected CostTypePK costTypePK;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "TYPE_NAME")
    private String typeName;
    @Basic(optional = false)
    @NotNull
    @Column(name = "TYPE_STATUS")
    private Character typeStatus;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "costType")
    private List<CostClass> costClassList;

CostTypePK.class

@Embeddable
public class CostTypePK implements Serializable {
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 2)
    @Column(name = "DPTO_COD")
    private String dptoCod;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 5)
    @Column(name = "COST_COD")
    private String costCod;

CostClass.class

@Entity
@Table(name = "COST_CLASS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "CostClass.findAll", query = "SELECT c FROM CostClass c"),
    @NamedQuery(name = "CostClass.findByCostType", query = "SELECT c FROM CostClass c WHERE c.costType IN :costTypes")})
public class CostClass implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 8)
    @Column(name = "CLASS_ID")
    private String classId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "CLASS_NAME")
    private String className;
    @Basic(optional = false)
    @NotNull
    @Column(name = "CLASS_STATUS")
    private Character classStatus;
    @JoinColumns({
        @JoinColumn(name = "DPTO_COD", referencedColumnName = "DPTO_COD"),
        @JoinColumn(name = "COST_COD", referencedColumnName = "COST_COD")})
    @ManyToOne(optional = false)
    private CostType costType;

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="DemoApp-ejbPU" transaction-type="JTA">
        <jta-data-source>jdbc/DemoDS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jtaDataSource" value="jdbc/DemoDS"/>
            <property name="eclipselink.logging.parameters" value="true"/>
            <property name="eclipselink.logging.logger" value="DefaultLogger"/>
            <property name="eclipselink.logging.level.sql" value="FINE"/>
        </properties>
    </persistence-unit>
</persistence>

如何运行我的命名查询

public List<CostClass> getCostClassByMyFilter() {
    List<CostType> costTypes = new ArrayList<>();
    CostTypePK costTypePK = new CostTypePK("RH", "RH001");
    CostType costType = new CostType(costTypePK);
    costTypes.add(costType);
    costTypePK = new CostTypePK("RH", "RH002");
    costType = new CostType(costTypePK);
    costTypes.add(costType);
    return em.createNamedQuery("CostClass.findByCostType", CostClass.class).setParameter("costTypes", costTypes).getResultList();
}

Logger FINE中的结果

Information:   [EL Fine]: sql: 2016-09-01 12:54:23.595--ServerSession(294472944)--Connection(1769598984)--SELECT CLASS_ID, CLASS_NAME, CLASS_STATUS, DPTO_COD, COST_COD FROM COST_CLASS WHERE ((?, ?) IN ((?,?),(?,?)))
    bind => [null, null, RH, RH001, RH, RH002]

0 个答案:

没有答案