我有两个与ManyToMany
相关的实体,我想通过命名查询来选择它们。这适用于我的测试(使用H2 DB设置)并在运行时抛出异常(设置了postgresql)。除了H2和PG之外,我很难找到测试和生产之间的差异。
实体和查询看起来如此(缩写):
@Entity(name = "Enrichment")
@Table(name = "mh_Enrichment")
NamedQueries({
@NamedQuery(name = "findByLink",
query = "SELECT e FROM Enrichment e INNER JOIN e.links l WHERE l.link in (:links)") })
public class EnrichmentImpl {
@Id
@Column(name = "enrichmentId")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToMany
@JoinTable(name = "mh_EnrichmentLinks", joinColumns = { @JoinColumn(name = "EnrichmentId",
referencedColumnName = "enrichmentId") }, inverseJoinColumns = { @JoinColumn(name = "Link",
referencedColumnName = "link") })
private List<Link> links;
}
@Entity(name = "Link")
@Table(name = "mh_enrichment_link")
public class LinksImpl {
@Id
@Column(name = "link", length = 1024)
private String link;
}
在生产中使用String值运行查询时,我得到:
Internal Exception: org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = bigint
Hinweis: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 215
Error Code: 0
Call: SELECT t1.enrichmentId FROM mh_enrichment_link t0, mh_EnrichmentLinks t2, mh_Enrichment t1 WHERE ((t0.link IN (?)) AND ((t2.EnrichmentId = t1.enrichmentId) AND (t0.link = t2.Link)))
任何想法有什么不对?这是查询,不是吗? 该查询应该检索与给定链接相关的富集列表。
更新#1 根据要求:数据库中的表格如下所示:
对于实体Link
CREATE TABLE mh_enrichment_link
(
link character varying(1024) NOT NULL,
CONSTRAINT mh_enrichment_link_pkey PRIMARY KEY (link)
)
对于实体Enrichment
CREATE TABLE mh_enrichment
(
enrichmentid bigint NOT NULL,
CONSTRAINT mh_enrichment_pkey PRIMARY KEY (enrichmentid)
)
对于关系(请参阅答案,这是它出错的地方)
CREATE TABLE mh_enrichmentlinks
(
link character varying(1024) NOT NULL,
CONSTRAINT mh_enrichment_link_pkey PRIMARY KEY (link)
)
答案 0 :(得分:1)
通过删除所有相关表并让JPA重新生成它们来解决此问题。表定义与实体定义不匹配。 这也很明显是测试工作和生产没有的原因。在测试中,表是在运行时生成的,在生产中它们已经存在(具有过时的定义)。 旁注:查询是正确的,并做了它应该做的事情。