【发布时间】:2022-11-23 05:22:38
【问题描述】:
我是 JPA 的新手,我需要一些关于我想做的查询的帮助。
这是我的实体:
服务实体
@Entity
@Table(name = "service", schema = "db2")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "serviceType")
public abstract class Service implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idService;
@ManyToMany
@JoinTable(name = "pack-serv", schema = "db2", joinColumns = @JoinColumn(name = "idService"), inverseJoinColumns = @JoinColumn(name = "idPackage"))
private List<Package> packages;
public Service() {
}
//getter and setter of all attributes
}
固定互联网实体
@Entity
@Table(name = "service", schema = "db2")
@DiscriminatorValue("Fixed Internet")
public class FixedInternet extends Service implements Serializable {
private static final long serialVersionUID = 1L;
private int GB;
private int feeExtraGB;
@Column(name="serviceType", insertable = false, updatable = false)
protected String type;
public FixedInternet() {
super();
}
//getter and setter of all attributes
}
我想检索与特定包 ID 相关的所有固定互联网服务。我做了这个查询:
@NamedQuery(name = "FixedInternet.findServicesByPackID", query = "SELECT s.GB, s.feeExtraGB, s.idService FROM FixedInternet s JOIN s.packages p WHERE p.idPackage = ?1 AND s.type LIKE 'Fixed Internet'")
但是当我运行它时,会出现与 SQL 语法相关的错误:
Internal Exception: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-serv t2, db2.package t1 WHERE ((((t1.IDPACKAGE = 1) AND t0.serviceType LIKE 'Fi' at line 1
Error Code: 1064
Call: SELECT t0.GB, t0.FEEEXTRAGB, t0.IDSERVICE FROM db2.service t0, db2.pack-serv t2, db2.package t1 WHERE ((((t1.IDPACKAGE = ?) AND t0.serviceType LIKE ?) AND (t0.serviceType = ?)) AND ((t2.idService = t0.IDSERVICE) AND (t1.IDPACKAGE = t2.idPackage)))
bind => [3 parameters bound]
我无法解决它,有人可以帮助我了解我错了吗?
这是数据库的ER图,如果有帮助的话:ER Diagram
更新:没有“s.type LIKE 'Fixed Internet'”的部分,错误是一样的:
Internal Exception: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-serv t2, db2.package t1 WHERE ((((t1.IDPACKAGE = 1) AND t0.serviceType LIKE 'Fi' at line 1
Error Code: 1064
Call: SELECT t0.GB, t0.FEEEXTRAGB, t0.IDSERVICE FROM db2.service t0, db2.pack-serv t2, db2.package t1 WHERE (((t1.IDPACKAGE = ?) AND (t0.serviceType = ?)) AND ((t2.idService = t0.IDSERVICE) AND (t1.IDPACKAGE = t2.idPackage)))
bind => [2 parameters bound]
【问题讨论】:
-
请将所有相关代码添加到
@NamedQuery。该错误似乎与您发布的内容不符 -
为什么要在查询中添加“AND s.type LIKE 'Fixed Internet'”?在 FixedInternet 上查询会自动添加过滤器,因此您只能返回 FixedInternet 实例。仅当您想从结果中排除潜在的 FixedInternet 子类时才需要这样做,并且您应该为“where TYPE(s) = FixedInternet”使用 JPQL 'TYPE' 表达式
-
@PanagiotisBougioukos 查询是我写的那个,它已经完成,没有别的了..
-
@Chris 我也尝试不添加“AND s.type LIKE 'Fixed Internet',但错误几乎相同(我用没有那部分的结果更新了问题)
-
错误与正在执行的查询不匹配 - 括号太多,错误状态是使用 Like 而不是 '='。看起来太像你的旧查询了,比如语句被缓存并被重用 - 你是否可能剪切并粘贴了旧的错误字符串?否则,请尝试干净重启,并可能打开 SQL 和参数日志记录以查看在发出错误之前执行的语句。奇怪的是它以“-”开头。您的数据库的表名中是否允许使用该字符?您可以尝试更改表名以避免使用“-”,而是使用“_”。
标签: java jpa inheritance jpql