最近在研究cuba platform的使用,目前在测试chart部分的功能。使用chart展示的时候经常的会在查询中使用分组之类的语句。目前遇到这样的问题,写了一条jpql语句如下:
select e.device.deviceType as deviceType, count(e.device.deviceType) as amount from cubademo_Event e group by e.device.deviceType这里是测试根据设备类型分组查看每种设备类型的故障统计。但是应用提示IllegalStateException: query path ‘e.device.deviceType’ is unresolved,是否是因为jpql不支持这种方式的查询。如果不支持的话是否只能够创建相关的service类,使用entitymanager来执行sql语句获取结果了?
看错误是找不到 Event.device.deviceType
这个实体关系。请提供一下 Event 和 Device 这两个实体的定义?
public class DeviceType extends StandardEntity {
private static final long serialVersionUID = 4554084912978370442L;
@Column(name = "NAME")
protected String name;
@Column(name = "CODE")
protected String code;
@Column(name = "MEME")
protected String memo;
public String getName() {
return name;
}
Device的定义:
public class Device extends StandardEntity {
private static final long serialVersionUID = -5163569954783812322L;
@Lookup(type = LookupType.DROPDOWN,actions = {"open"})
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "DEVICE_TYPE_ID")
protected DeviceType deviceType;
@Column(name = "MEMO")
protected String memo;
}
Event的定义:
public class Event extends StandardEntity {
private static final long serialVersionUID = -1433499989068351118L;
@Lookup(type = LookupType.DROPDOWN,actions = {"open"})
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "DEVICE_ID")
protected Device device;
@Lookup(type = LookupType.DROPDOWN,actions = {"open"})
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "FAULT_REASON_ID")
protected FaultReason faultReason;
public Device getDevice() {
return device;
}
}
想通过对Event的统计制作一些图表。例如根据设备类型进行统计等等。
谢谢,你贴了两个 Device 类,Event 类没有提供
不好意思,耽误您的时间了。Event类的定义如下:
public class Event extends StandardEntity {
private static final long serialVersionUID = -1433499989068351118L;
@Lookup(type = LookupType.DROPDOWN,actions = {"open"})
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "DEVICE_ID")
protected Device device;
@Lookup(type = LookupType.DROPDOWN,actions = {"open"})
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "FAULT_REASON_ID")
protected FaultReason faultReason;
public Device getDevice() {
return device;
}
你好,我用 7.1Beta3 测试了这个 jpql,没有出现问题。我是用 Studio 创建的 CUBA entity 类: entity 文件:
entities.zip (1.7 KB)
service 中的方法如下:
@Override
public Object countDeviceByType() {
List result;
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
Query q = em.createQuery("select e.device.deviceType.code as deviceType, count(e.device.deviceType) as amount from forum_Event e group by e.device.deviceType");
result = q.getResultList();
}
return result;
}
Rest API 调用的输出:
感谢回复。重新创建了一个应用,使用7.1.0Beta3的框架,上述查询好用了,然后将框架降至7.0.10也没问题。挺奇怪的一个问题。目前还没有找到是什么原因。
可以尝试用 CUBA->Build Tasks-> clean
清除一下项目的缓存 class 文件,然后再删除项目目录下的 deploy
文件夹,再重启项目试试。