我按照這個例子,將 Student 重命名為 Journey,將 Library 重命名為 Station。我對錯誤的解釋是,表模式沒有名為 'departure' 的欄位,而 java 類表示有。我沒想到這是一個問題,因為我遵循了這個答案中提到的 JPA 命名約定。在示例的情況下, lib b_id == LIB_B_ID 。在我的情況下,出發 station_id == 出發_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;
}
}
uj5u.com熱心網友回復:
與我交叉參考A和B的兩個示例相反,我在這個問題中注意到@ManyToOne 注釋在 getter 之上,而不是欄位的宣告。因為我當前作業的注釋(@id 和 @GeneratedValue)也在 getter 之上,所以它似乎是一個合理的解決方案。將注釋移動到 getter 后,錯誤消失了。
這些示例并沒有錯,它們只是使用了與我通過將 @id 注釋置于 getter 之上而無意中隱式設定的訪問策略不同的訪問策略。通過將我的@id 注釋放在 getter 之上,我隱式指定了基于屬性的訪問,從而導致休眠呼叫 getter 和 setter 方法來訪問我的屬性,因此為什么我的 @ManyToOne 注釋必須在 getter 之上。我參考的示例將 @id 注釋放在類變數本身之上,隱式指定基于欄位的訪問,在這種情況下,您應該將 @ManyToOne 注釋放在類變數作業之上。來源
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/363965.html
上一篇:spring-data-多個DB:NoUniqueBeanDefinitionException沒有可用的“TransactionManager”型別的合格bean
