我正在構建一個 Spring Boot 應用程式,但我遇到了JpaSystemException: attempted to assign id from null one-to-one property錯誤。讓我給你一些關于這個專案的細節。
首先,我有以下資料庫架構
describe airports;
-------------------------- ------------ ------ ----- --------- -------
| Field | Type | Null | Key | Default | Extra |
-------------------------- ------------ ------ ----- --------- -------
| airport_id | int(11) | NO | PRI | NULL | |
| airport_name | text | NO | | NULL | |
-------------------------- ------------ ------ ----- --------- -------
describe flights;
------------------------ --------- ------ ----- --------- -------
| Field | Type | Null | Key | Default | Extra |
------------------------ --------- ------ ----- --------- -------
| flight_from_airport_id | int(11) | NO | PRI | NULL | |
| flight_to_airport_id | int(11) | NO | PRI | NULL | |
| flight_type | int(11) | NO | PRI | NULL | |
------------------------ --------- ------ ----- --------- -------
基本上,表航班用于存盤從一個機場到另一個機場的航班資料。
我有兩個物體;機場和航班如下:
Airport.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "airports")
public class Airport {
@Id
@Column(name="airport_id")
private int airportId;
@Column(name="airport_name")
private String airportName;
//Getters and Setters
public Airport(int airportId, String airportName) {
this.airportId = airportId;
this.airportName = airportName;
}
}
Flight.java
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.Table;
@Entity
@Table(name = "flights")
public class Flight {
@EmbeddedId
FlightPrimaryKey flightPrimaryKey;
@MapsId("fromAirport")
@ManyToOne
@JoinColumn(name = "flightFromAirportId")
private Airport fromAirport;
@MapsId("toAirport")
@ManyToOne
@JoinColumn(name = "flightToAirportId")
private Airport toAirport;
@Column(name="flight_type")
private int flightType;
//Getters and Setters
public Flight(FlightPrimaryKey flightPrimaryKey, int flightType) {
this.flightPrimaryKey = flightPrimaryKey;
this.flightType = flightType;
}
}
因為我想要一個類 Flight 的復合鍵,所以我有一個 Id 類。
FlightId.java
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class FlightId implements Serializable {
@Column(name = "flight_from_airport_id")
private int fromAirport;
@Column(name = "flight_to_airport_id")
private int toAirport;
// Getters and Setters, hashcode, equals
public TransferPrimaryKey(int fromAirport, int toAirport) {
this.fromAirport = fromAirport;
this.toAirport = toAirport;
}
}
然后我有一個 JPA 存盤庫
FlightRepository.java
public interface FlightRepository extends JpaRepository<Flight, FlightId>{}
為了測驗上述內容,我使用以下代碼:
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class FlightTest {
@Autowired
private AiportRepository airportRepository;
@Autowired
private FlightRepository flightRepository;
@Test
public void shouldStoreAFlight() {
Airport fromAirport = airportRepository.save(new Airport(1, "LAX");
Airport toAirport = airportRepository.save(new Airport(2, "JFK")
FlightId flightId = new FlightId(fromAirport.getId(), toAirport.getId());
Flight flight = flightRepository.save(new Flight(flightId, 1));
}
}
當我運行上面的代碼時,我得到了 JpaSystemException。我也無法理解為什么該錯誤是關于一對一屬性的。我只定義了一個多對一的屬性。顯然我錯過了一些東西。
uj5u.com熱心網友回復:
解決方案應該是設定您的參考:
public void shouldStoreAFlight() {
Airport fromAirport = airportRepository.save(new Airport(1, "LAX");
Airport toAirport = airportRepository.save(new Airport(2, "JFK")
FlightId flightId = new FlightId(fromAirport.getId(), toAirport.getId());
Flight flight = new Flight(flightId, 1);
flight.setFromAirport(fromAirport);
flight.setToAirport(toAirport);
flight = flightRepository.save(flight);
}
不需要使用上面的代碼設定 FlightId 中的值,但我將其保留為與您的建構式匹配,并且因為您將它們設定為int而不是允許空值的 Integer。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/437816.html
