SELECT ohne IdClass-Annotation

krgewb

Top Contributor
Ich habe folgende Klassen:
Java:
package com.a.vision.backend.database.entities;

import javax.persistence.*;
import java.util.Date;

@Entity
@IdClass(FrameMonitorDataEntityPK.class)
@Table(name = "FRAME_MONITOR_DATA")
public class FrameMonitorDataEntity {

    @Id
    private Long videoSourceId;

    @Id
    private Integer channel;

    @Id
    private Date lastFrameTs;

    @Column(name = "VIEW_ID", nullable = false)
    private Long viewId;

    public Long getVideoSourceId() {
        return videoSourceId;
    }

    public void setVideoSourceId(Long videoSourceId) {
        this.videoSourceId = videoSourceId;
    }

    public Integer getChannel() {
        return channel;
    }

    public void setChannel(Integer channel) {
        this.channel = channel;
    }

    public Date getLastFrameTs() {
        return lastFrameTs;
    }

    public void setLastFrameTs(Date lastFrameTs) {
        this.lastFrameTs = lastFrameTs;
    }

    public Long getViewId() {
        return viewId;
    }

    public void setViewId(Long viewId) {
        this.viewId = viewId;
    }
}

Java:
package com.a.vision.backend.database.entities;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Date;

@Embeddable
public class FrameMonitorDataEntityPK implements Serializable {

    @Column(name = "VIDEO_SOURCE_ID", nullable = false)
    private Long videoSourceId;

    @Column(name = "CHANNEL", nullable = false)
    private Integer channel;

    @Column(name = "LAST_FRAME_TS", columnDefinition = "datetime(6)", nullable = false)
    private Date lastFrameTs;

    public Long getVideoSourceId() {
        return videoSourceId;
    }

    public void setVideoSourceId(Long videoSourceId) {
        this.videoSourceId = videoSourceId;
    }

    public Integer getChannel() {
        return channel;
    }

    public void setChannel(Integer channel) {
        this.channel = channel;
    }

    public Date getLastFrameTs() {
        return lastFrameTs;
    }

    public void setLastFrameTs(Date lastFrameTs) {
        this.lastFrameTs = lastFrameTs;
    }

}

Hier mache ich meinen SELECT:

Java:
@Override
public List<FrameMonitorDataDTO> findFrameMonitorDataById(IdWrapper idWrapper) {
    Query query = entityManager.createQuery("SELECT f from FrameMonitorDataEntity f where f.videoSourceId = ?1 and f.viewId = ?2 and f.channel = ?3 order by f.lastFrameTs desc");
    query.setParameter(1, Long.parseLong(idWrapper.getVideoSourceId5()));
    query.setParameter(2, Long.parseLong(idWrapper.getViewId5()));
    query.setParameter(3, idWrapper.getChannel5());
    return FrameMonitorDataWrapper.toDTO(query.getResultList());
}

Das funktioniert. Ich würde aber gerne machen, dass meine Klasse wie folgt aussieht. Dann funktioniert aber meine findFrameMonitorDataById nicht mehr.

Java:
package com.a.vision.backend.database.entities;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "FRAME_MONITOR_DATA")
public class FrameMonitorDataEntity {

    @EmbeddedId
    private FrameMonitorDataEntityPK pk;

    @Column(name = "VIEW_ID", nullable = false)
    private Long viewId;

    public FrameMonitorDataEntityPK getPk() {
        return pk;
    }

    public void setPk(FrameMonitorDataEntityPK pk) {
        this.pk = pk;
    }

    public Long getViewId() {
        return viewId;
    }

    public void setViewId(Long viewId) {
        this.viewId = viewId;
    }
}
 
Mit javax-Packages und eclipselink musste ich das früher immer so definieren:
Java:
    @EmbeddedId
    @AttributeOverrides({
        @AttributeOverride(name = "sjId", column = @Column(name = "SJ_ID", nullable = false, precision = 2, scale = 0)),
        @AttributeOverride(name = "eId", column = @Column(name = "E_ID", nullable = false, precision = 4, scale = 0)),
        @AttributeOverride(name = "sort", column = @Column(name = "SORT", nullable = false, precision = 126, scale = 0)) })
    private BsKommunikationId id;

Java:
@Embeddable
public class BsKommunikationId implements Serializable {

    private static final long serialVersionUID = ...

    @Column(name = "SJ_ID", nullable = false, precision = 2, scale = 0)
    private int sjId;

    @Column(name = "E_ID", nullable = false, precision = 4, scale = 0)
    private int eId;

    @Column(name = "SORT", nullable = false, precision = 126, scale = 0)
    private double sort;

    ...
}
 

Neue Themen


Zurück
Oben