JPA一个实体3表

时间:2013-05-11 16:28:41

标签: java inheritance jpa entity mappedsuperclass

我有三张桌子,每张桌子都有相同的颜色。其中一个具有原始测量数据,另一个具有聚合到分钟和小时的数据。在JPA中加载数据时,我想为每组数据提供相同的实体/类。我用继承/ mappedsuperclass尝试了它,但到目前为止还没有解决(不确定我是否也正确)。我要么得到没有合适的构造函数或表没有映射。当我将Table of my Value实体设置为其中一个表时,它适用于该表,但我希望能够在运行时从所有表中加载数据。

来自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;
}

Value.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

希望你能理解我想要做的事情。

0 个答案:

没有答案