使用组合键时出现 JpaSystemException
我正在构建一个 Spring Boot 应用程序,但遇到了 JpaSystemException: attempts to allocate 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。我也无法理解为什么错误说的是一对一属性。我只定义了一个多对一的属性。显然我错过了一些东西。
I am building a spring boot application and I am stuck with the JpaSystemException: attempted to assign id from null one-to-one property
error. Let me give you some details about the project.
First of all, I have the following db schema
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 | |
+------------------------+---------+------+-----+---------+-------+
Basically, table flights is used to store data for flights from one airport to another.
I have two entities; airport and flight as follows:
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;
}
}
Because I want a composite key for the class Flight, I have an Id class.
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;
}
}
Then I have a JPA repository
FlightRepository.java
public interface FlightRepository extends JpaRepository<Flight, FlightId>{}
And to test the above I use the following code:
@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));
}
}
When I run the above code, I get the JpaSystemException. I also cannot understand why the error says about one-to-one property. I have only defined a many-to-one property. Clearly I am missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案应该是设置您的引用:
应该不需要使用上面的代码设置 FlightId 中的值,但我将其保留为与您的构造函数匹配,并且因为您将它们设置为 int 而不是允许空值的整数。
Solution should be to set your references:
There should be no need to set the values in FlightId with the above code, but I left it to match your constructors, and because you have them set to int instead of an Integer that allows nulls.