我正在使用Hibernate,MySQL,Spring MVC。
我目前的工作是预订机票。
我从路径变量获得了航班时刻表实体。而且,我从HttpSession获得了一个乘客实体。
以下是控制器代码:
@RequestMapping("/booking/{results[0].flightscheduleId}")
public String bookBoardingPass
(@PathVariable("results[0].flightscheduleId") Integer flightId, HttpSession session) {
Passenger passenger = (Passenger) session.getAttribute("user");
FlightSchedule flightSchedule = this.flightScheduleService.get(flightId);
this.boardingPassService.booking(flightSchedule, passenger);
return "search";
}
以下是预订方法代码:
@Override
public void booking (FlightSchedule flightSchedule, Passenger passenger) {
BoardingPass ticket = new BoardingPass();
ticket.setFlightschedule(flightSchedule);
ticket.setPassenger(passenger);
System.out.println("flight id: "+flightSchedule.getFlightscheduleId());
System.out.println("passenger id: "+passenger.getPassengerId());
this.persist(ticket);
//sessionFactory.getCurrentSession().saveOrUpdate(ticket);
}
在预订方式中,我可以同时打印flightSchedule
个ID和passenger
的ID。
但是,当我执行'坚持'方法,它说Field 'flightschedule_id' doesn't have a default value
在persist方法中,它运行sessionFactory.getCurrentSession().saveOrUpdate(ticket);
我不能给它一个默认值。另外,我通过谷歌搜索结果检查phpmyadmin上的STRICT模式(?)。 (我不确定我做得好。无论如何它都没有用)。
我该如何解决这个问题?
编辑:
以下是BoardingPass课程:
@Entity
@Table
public class BoardingPass implements java.io.Serializable {
private Integer boardingpassId;
private FlightSchedule flightschedule;
private Passenger passenger;
public BoardingPass() {
}
public BoardingPass(FlightSchedule flightschedule, Passenger passenger) {
this.flightschedule = flightschedule;
this.passenger = passenger;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getBoardingpassId() {
return this.boardingpassId;
}
public void setBoardingpassId(Integer boardingpassId) {
this.boardingpassId = boardingpassId;
}
@ManyToOne
@MapsId("flightscheduleId")
public FlightSchedule getFlightschedule() {
return this.flightschedule;
}
public void setFlightschedule(FlightSchedule flightschedule) {
this.flightschedule = flightschedule;
}
@ManyToOne
@MapsId("passengerId")
public Passenger getPassenger() {
return this.passenger;
}
public void setPassenger(Passenger passenger) {
this.passenger = passenger;
}
}
以防万一,我添加了FlightSchedule和Passenger类的一部分。
下面是FlightSchedule.java:
@Entity
@Table
public class FlightSchedule implements java.io.Serializable {
private Integer flightscheduleId;
private Date depDay;
private Date depTime;
private Date arrDay;
private Date arrTime;
private Double flightTime;
private Integer price;
private Set<BoardingPass> boardingPasses = new HashSet<BoardingPass>(0);
private Airplane airplane;
private Airport depAirport;
private Airport arrAirport;
public FlightSchedule() {
}
public FlightSchedule(Date depDay, Date depTime, Date arrDay, Date arrTime, Integer price) {
this.depDay = depDay;
this.depTime = depTime;
this.arrDay = arrDay;
this.arrTime = arrTime;
this.price = price;
}
public FlightSchedule(Date depDay, Date depTime, Date arrDay, Date arrTime, Double flightTime, Integer price,
Set<BoardingPass> boardingPasses, Airplane airplane, Airport depAirport, Airport arrAirport) {
this.depDay = depDay;
this.depTime = depTime;
this.arrDay = arrDay;
this.arrTime = arrTime;
this.flightTime = flightTime;
this.price = price;
this.boardingPasses = boardingPasses;
this.airplane = airplane;
this.depAirport = depAirport;
this.arrAirport = arrAirport;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getFlightscheduleId() {
return this.flightscheduleId;
}
public void setFlightscheduleId(Integer flightscheduleId) {
this.flightscheduleId = flightscheduleId;
}
@OneToMany(mappedBy = "flightschedule", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<BoardingPass> getBoardingPasses() {
return this.boardingPasses;
}
public void setBoardingPasses(Set<BoardingPass> boardingPasses) {
this.boardingPasses = boardingPasses;
}
.... getters and setters...
}
下面是Passenger.java:
@Entity
@Table
public class Passenger implements java.io.Serializable {
private Integer passengerId;
private String password;
private String passportNo;
private String passengerName;
private Date birth;
private Integer mileage;
private String contact;
private Set<BoardingPass> boardingPasses = new HashSet<BoardingPass>(0);
public Passenger() {
}
public Passenger(String password, String passportNo, Date birth, Integer mileage) {
this.password = password;
this.passportNo = passportNo;
this.birth = birth;
this.mileage = mileage;
}
public Passenger(String password, String passportNo, String passengerName, Date birth, Integer mileage, String contact, Set<BoardingPass> boardingPasses) {
this.password = password;
this.passportNo = passportNo;
this.passengerName = passengerName;
this.birth = birth;
this.mileage = mileage;
this.contact = contact;
this.boardingPasses = boardingPasses;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getPassengerId() {
return this.passengerId;
}
public void setPassengerId(Integer passengerId) {
this.passengerId = passengerId;
}
@OneToMany(mappedBy = "passenger", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<BoardingPass> getBoardingPasses() {
return this.boardingPasses;
}
public void setBoardingPasses(Set<BoardingPass> boardingPasses) {
this.boardingPasses = boardingPasses;
}
.... getters and setters...
}
答案 0 :(得分:1)
我认为该映射完全错误的第一件事是@MapsId注释:
这些用于告诉hibernate关系属性对应的复合键中的属性。
没有复合主键的含义它没有任何意义,我怀疑它会弄乱你的映射...尝试删除它们并看看它是怎么回事
一个简单的例子http://www.objectdb.com/api/java/jpa/MapsId
P.S。我忘了你需要将@JoinColumn注释添加到映射
@JoinColumn(name = "flightschedule_id", referencedColumnName = "boardingPass_id")
编辑: 导致问题的原因还在于您指定了db列的名称错误
@Column(name = "id", unique = true, nullable = false)
public Integer getFlightscheduleId() {
return this.flightscheduleId;
}
不应该是@Column(name = "flightschedule_id", unique = true, nullable = false)
?
其他2个实体的相同内容也检查@Column
name属性是否对应于db列名
答案 1 :(得分:0)
如果您未将PRIMARY KEY
列指定为AUTO_INCREMENT
,则必须在数据库中手动提供值。将您的flightschedule_id
列作为主键AUTO_INCREMENT
,然后它应该有效。
希望这会有所帮助。谢谢。