spring-boot - 尝试多对一实体映射时出现未知列错误
问题描述
我按照这个例子,将 Student 重命名为 Journey,并将 Library 重命名为 Station。我对错误的解释是,表模式没有名为“departure”的字段,而 java 类表示有。我没想到这会成为问题,因为我遵循了此答案中提到的 JPA 命名约定。在示例的情况下, lib + b_id == LIB_B_ID 。在我的例子中,department + station_id == shipping_station_id(我的数据库命名方案使用小写)。
该示例的其他显着差异是,我没有 persistence.xml,对于 id,我使用 GenerationType.IDENTITY 而不是 GenerationType.AUTO。
错误
java.sql.SQLException: Unknown column 'departure' in 'field list'
架构脚本
CREATE TABLE `station` (
`station_id` bigint(20) NOT NULL AUTO_INCREMENT,
`locker_id` bigint(20) DEFAULT NULL,
`station_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`station_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2579 DEFAULT CHARSET=utf8
CREATE TABLE `journey` (
`journey_id` bigint(20) NOT NULL AUTO_INCREMENT,
`departure_station_id` bigint(20) DEFAULT NULL,
`destination_station_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`journey_id`),
KEY `station_id_departure` (`departure_station_id`),
KEY `station_id_destination` (`destination_station_id`),
CONSTRAINT `journey_ibfk_1` FOREIGN KEY (`destination_station_id`) REFERENCES `station` (`station_id`),
CONSTRAINT `journey_ibfk_2` FOREIGN KEY (`departure_station_id`) REFERENCES `station` (`station_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
旅行
@Entity
public class Journey implements Serializable{//Serializable required by @JoinColumn
//Station
public Journey(Station departure, Station destination){
this.departure = departure;
this.destination = destination;
}
//List<Station>
public Journey(List<Station> journeyStationList){
departure = journeyStationList.get(0);//0 is first index
destination = journeyStationList.get(journeyStationList.size() - 1);//-1 because first index of List is 0
}
private Long journeyId;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getJourneyId(){
return journeyId;
}
public void setJourneyId(Long journeyId){
this.journeyId = journeyId;
}
@ManyToOne
private Station departure;
public Station getDeparture(){
return departure;
}
public void setDeparture(Station departure){
this.departure = departure;
}
@ManyToOne
private Station destination;
public Station getDestination(){
return destination;
}
public void setDestination(Station destination){
this.destination = destination;
}
}
车站
@Entity
public class Station implements Serializable{//Serializable required by @JoinColumn
public Station(){//default constructor needed for JPA query
}
public Station(String stationName){
setStationName(stationName);
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long stationId;
public Long getStationId(){
return stationId;
}
public void setStationId(Long stationId){
this.stationId = stationId;
}
private String stationName;
public String getStationName(){
return stationName;
}
public void setStationName(String stationName){
this.stationName = stationName;
}
}
解决方案
与我交叉引用A和B的两个示例相比,我注意到在这个问题中 @ManyToOne 注释位于 getter 之上,而不是字段声明。因为我当前工作的注释(@id 和@GeneratedValue)也在吸气剂之上,这似乎是一个合理的解决方案。将注释移动到 getter 后,错误消失了。
这些示例并没有错,它们只是使用了不同的访问策略,而不是我通过将 @id 注释放置在 getter 上方而无意中隐式设置的访问策略。通过将 @id 注释放在 getter 上方,我隐式指定了基于属性的访问,从而导致休眠调用 getter 和 setter 方法来访问我的属性,因此我的 @ManyToOne 注释必须位于 getter 上方。我引用的示例将@id 注释放在类变量本身之上,隐式指定基于字段的访问,在这种情况下,您应该将@ManyToOne 注释放在类变量之上。来源
推荐阅读
- rounding-error - 为什么会出现舍入错误?
- c - ANTLR4 解析器生成器可以生成 C 代码吗?
- java - 如何访问另一个类的方法变量?
- javascript - React 没有将行附加到表体
- anychart - 如何获得烛台上的当前点值?
- bots - 将 embed 的图片设置为作者个人资料图片 Discord.js
- php - 无法上传文件加载资源失败:服务器响应状态为503
- python - 打印到 PDF 无法在 Linux Headless chrome 浏览器上运行,用于 python 上的 seleminum
- javascript - 未在完整日历中显示的事件
- c# - 没有框架将 TabbedPage 绑定到 ViewModel