【发布时间】:2023-04-04 11:58:01
【问题描述】:
我有很多类的注释放在 getter 上方,如下所示:
private Location location;
@ManyToOne(fetch = FetchType.LAZY)
@NotNull
@JoinColumn(name = "LocationId", nullable = false)
public Location getLocation() {
return location;
}
该代码适用于 hibernate \ dto 架构,但我正在尝试实现 Spring Data 并且我没有对代码进行任何更改,我只是在依赖项中添加了 spring-data 并且我开始收到错误:
Caused by: org.hibernate.MappingException: Unable to find column with logical name locationid in table LocationProperty
at org.hibernate.cfg.Configuration$MappingsImpl.getPhysicalColumnName(Configuration.java:2949)
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.addConstraintToColumn(IndexOrUniqueKeySecondPass.java:86)
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.doSecondPass(IndexOrUniqueKeySecondPass.java:76)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1597)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1792)
可以通过将注释从 getter 移动到字段声明来解决,所以这段代码可以正常工作:
@ManyToOne(fetch = FetchType.LAZY)
@NotNull
@JoinColumn(name = "LocationId", nullable = false)
private Location location;
public Location getLocation() {
return location;
}
Spring-data 是 1.3.0.RELEASE,我知道它已经很老了,但是在这个项目中我们使用的是旧的休眠,所以我不能添加新的,因为我在会话工厂等方面遇到了大量的错误。所以这不是一个选项。
休眠是 4.1.12.final
我可以通过移动注释来解决这个问题,但是有数百个类,所以这真的是个假工作。
【问题讨论】:
-
这个错误告诉你LocationProperty表上没有名为locationid的列。
-
@sovannarithcheav 真的吗?为什么上面的代码有效? (提示:bc 有这样的专栏)
标签: java spring hibernate orm annotations