0

我有三个表,每个表都有相同的列。其中一个具有原始测量数据,另一个具有聚合到分钟和小时的数据。在 JPA 中加载数据时,我希望每组数据都具有相同的实体/类。我用继承/映射超类尝试了它,但到目前为止还没有成功(不确定我是否做对了)。我要么没有合适的构造函数,要么表没有映射。当我将我的值实体的表设置为其中一个表时,它适用于该表,但是我希望能够在运行时从所有表中加载数据。

来自 PersistenceManager.java

@SuppressWarnings("unchecked")
public List<Value> getValues(Sensor sensor) throws Exception {
    String query = "SELECT v FROM ValueByHour v WHERE v.sensor = :sensor ORDER BY v.timestamp ASC";
    List<Value> list = em.createQuery(query, Value.class).setParameter("sensor", sensor).getResultList();
    return list;
}

价值.java

package de.mediacoding.smarthome.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;


/**
 * The persistent class for the Value database table.
 * 
 */
@Entity
@Table(name="Value")
public class Value implements Serializable {

private static final long serialVersionUID = -7514236041356226101L;
private int id;
private Timestamp timestamp;
private double value;
private Sensor sensor;

public Value() {
}

public Value(int id, Timestamp timestamp, double value, Sensor sensor) {
    this.id = id;
    this.timestamp = timestamp;
    this.value = value;
    this.sensor = sensor;
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
public int getId() {
    return this.id;
}

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

@Column(nullable=false)
public Timestamp getTimestamp() {
    return this.timestamp;
}

public void setTimestamp(Timestamp timestamp) {
    this.timestamp = timestamp;
}

@Column(nullable=false)
public double getValue() {
    return this.value;
}

public void setValue(double value) {
    this.value = this.sensor.getRealValue(value);;
}

//bi-directional many-to-one association to Sensor
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="sensor_id", nullable=false)
public Sensor getSensor() {
    return this.sensor;
}

public void setSensor(Sensor sensor) {
    this.sensor = sensor;
}

}

堆栈跟踪

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: ValueByHour is not mapped [SELECT v FROM ValueByHour v WHERE v.sensor = :sensor ORDER BY v.timestamp ASC]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3291)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3180)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1735)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:301)
... 36 more

如果有帮助,这里是整个项目http://crafthost.de/src.zip

希望你能理解我想要做什么。

4

0 回答 0