JPA查询连接表

时间:2014-11-21 17:40:11

标签: java jpa join jpql

我有两个表:SegmentObservation。 一个段可以有多个观察值,1到N的关系(segment_id是Observation表中的外键)。我必须验证给定的时间戳和给定的段是否有观察结果。

观察课如下:

@Entity
@Table(name="observation")
@NamedQuery(name="Observation.findAll", query="SELECT o FROM Observation o")
public class Observation implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private int id;

@Column(nullable=false)
private byte observation;

@Column(name="observed_at", nullable=false)
private Timestamp observedAt;

//bi-directional many-to-one association to Segment
@ManyToOne
@JoinColumn(name="segment_id", nullable=false)
private Segment segment;

public Observation() {
}

public int getId() {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}

public byte getObservation() {
    return this.observation;
}

public void setObservation(byte observation) {
    this.observation = observation;
}

public Timestamp getObservedAt() {
    return this.observedAt;
}

public void setObservedAt(Timestamp observedAt) {
    this.observedAt = observedAt;
}

public Segment getSegment() {
    return this.segment;
}

public void setSegment(Segment segment) {
    this.segment = segment;
}
}

Segment表格如下:

@Entity
@Table(name="segment")
@NamedQuery(name="Segment.findAll", query="SELECT s FROM Segment s")
public class Segment implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.TABLE)
@Column(name="segment_id", unique=true, nullable=false)
private int segmentId;

private int dummy;

//bi-directional many-to-one association to Observation
@OneToMany(mappedBy="segment")
private List<Observation> observations;

public Segment() {
}

public int getSegmentId() {
    return this.segmentId;
}

public void setSegmentId(int segmentId) {
    this.segmentId = segmentId;
}

public int getDummy() {
    return this.dummy;
}

public void setDummy(int dummy) {
    this.dummy = dummy;
}

public List<Observation> getObservations() {
    return this.observations;
}

public void setObservations(List<Observation> observations) {
    this.observations = observations;
}

public Observation addObservation(Observation observation) {
    getObservations().add(observation);
    observation.setSegment(this);

    return observation;
}

public Observation removeObservation(Observation observation) {
    getObservations().remove(observation);
    observation.setSegment(null);

    return observation;
}
}

通常,在标准SQL中我会写:

 SELECT COUNT(*)
 FROM Observation o, Segment s
 WHERE o.segment_id = s.segment_id
 AND  s.segment_id = segmentIdParam
 AND o.observed_at = observedAtParam
 AND o.observation = observationParam

我想使用JPA连接编写此查询。我写了以下内容:

Query query = initEntityManager().createQuery(
                  "SELECT s "
                + "FROM Observation o JOIN o.Segment s "
                + "WHERE s.segmentId   = :segmentIdParam"
                +  " AND o.observedAt  = :observedAtParam"
                +  " AND o.observation = :observationParam", Observation.class);

    query.setParameter("segmentIdParam",   segmentId);
    query.setParameter("observedAtParam",  observedAt);
    query.setParameter("observationParam", observation);

不幸的是我得到如下例外:

java.lang.IllegalArgumentException: An exception occurred while creating a query in     EntityManager: 
 Exception Description: Problem compiling [SELECT s FROM Observation o JOIN o.Segment s WHERE s.segmentId = :segmentIdParam AND o.observedAt = :observedAtParam AND o.observation = :observationParam ]. 
[33, 42] The collection-valued path 'o.Segment' cannot be resolved to a valid association field.
[51, 62] The state field path 's.segmentId' cannot be resolved to a valid type.

应如何编写此JPA查询?

2 个答案:

答案 0 :(得分:2)

在某些(可能是全部)JPA实现中,字段区分大小写。

所以你的FROM子句应该是

FROM Observation o JOIN o.segment s

即。 o.segment而不是o.Segment

答案 1 :(得分:1)

尝试

SELECT o FROM Observarion o 
    WHERE o.segment =: segmentParam
      AND o.observedAt =: observedAt
      AND o.observation =: observationParam

在这种情况下,您应该发送类型为Segment的对象作为参数,而不是其ID。