我想在这个论坛上找到我的问题的答案。我的问题如下,我有两个类:
@Entity
@Table(name = "a")
public class A implements Serializable{
@Id
private String id = UUID.randomUUID().toString();
@Column(name = "REFERENCE_ID")
private String referenceId;
@Column(name = "VERSION")
private String version;
}
和
@Entity
@Table(name = "b")
public class B{
@Id
private String id = UUID.randomUUID().toString();
@Column(name = "REFERENCE")
private String reference;
@ManyToMany(fetch = FetchType.LAZY)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(value =
"(select r from A r where r.reference_id = reference_id order by r.version desc limit 1)",
referencedColumnName = "reference_id")),
@JoinColumnOrFormula(column = @JoinColumn(name = "reference_id",
referencedColumnName = "reference_id", insertable = false))
})
private A referenceId;
}
事情reference_id
不是 b 表中的唯一键,而只是表 A 中的指示性值,因此为了获取整个对应行,我必须使用连接注释中的公式进行一些过滤。当我尝试获取我的数据时,我收到以下错误
[请求处理失败;嵌套异常是 org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause org.postgresql.util.PSQLException: ERROR: 关系“a”不存在位置:309
编辑
实际上,将我的连接公式更改为
@JoinFormula(value =
"(select r from schema_A r where r.reference_id = reference_id order by r.version desc limit 1)",
referencedColumnName = "reference_id"))
现在的问题是该代码适用于多个环境,因为我的 application.yml 它看起来有点像这样;
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
default_schema: schema
jdbc:
lob:
non_contextual_creation: true
time_zone: UTC
感谢您的回复:)