org.hibernate.MappingException 在作为一对多关系一部分的实体之一中
org.hibernate.MappingException in one of the entity which is a part of one to many relationship
启动我的 spring 引导项目时出现以下异常 -
原因:org.hibernate.MappingException:无法确定类型:org.learning.api.Flights.Entities.Airport,在 table:flight_schedule,对于列:[org.hibernate.mapping.Column(目标)]
正在使用的实体如下。
航空公司
package org.learning.api.Flights.Entities;
import javax.persistence.*;
import java.util.Objects;
import java.util.Set;
@Entity(name="Airline")
public class Airline {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade=CascadeType.ALL,mappedBy = "airline")
private Set<FlightSchedule> flightSchedules;
public Airline() {
}
public Airline(Long id, String name, Set<FlightSchedule> flightSchedules) {
this.id = id;
this.name = name;
this.flightSchedules = flightSchedules;
}
public Airline(String name, Set<FlightSchedule> flightSchedules) {
this.name = name;
this.flightSchedules = flightSchedules;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<FlightSchedule> getFlightSchedules() {
return flightSchedules;
}
public void setFlightSchedules(Set<FlightSchedule> flightSchedules) {
this.flightSchedules = flightSchedules;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Airline airline = (Airline) o;
return Objects.equals(id, airline.id) && Objects.equals(name, airline.name) && Objects.equals(flightSchedules, airline.flightSchedules);
}
@Override
public int hashCode() {
return Objects.hash(id, name, flightSchedules);
}
@Override
public String toString() {
return "Airline{" +
"id=" + id +
", name='" + name + '\'' +
", flightSchedules=" + flightSchedules +
'}';
}
}
机场
package org.learning.api.Flights.Entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;
@Entity(name="Airport")
public class Airport {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String city;
public Airport() {
}
public Airport(Long id, String name, String city) {
this.id = id;
this.name = name;
this.city = city;
}
public Airport(String name, String city) {
this.name = name;
this.city = city;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Airport airport = (Airport) o;
return Objects.equals(id, airport.id) && Objects.equals(name, airport.name) && Objects.equals(city, airport.city);
}
@Override
public int hashCode() {
return Objects.hash(id, name, city);
}
@Override
public String toString() {
return "Airport{" +
"id=" + id +
", name='" + name + '\'' +
", city='" + city + '\'' +
'}';
}
}
航班
package org.learning.api.Flights.Entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;
@Entity(name = "Flight")
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String company;
private String modelName;
public Flight(){}
public Flight(Long id, String company, String modelName) {
this.id = id;
this.company = company;
this.modelName = modelName;
}
public Flight(String company, String modelName) {
this.company = company;
this.modelName = modelName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
@Override
public String toString() {
return "Flight{" +
"id=" + id +
", company='" + company + '\'' +
", modelName='" + modelName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Flight flight = (Flight) o;
return Objects.equals(id, flight.id) && Objects.equals(company, flight.company) && Objects.equals(modelName, flight.modelName);
}
@Override
public int hashCode() {
return Objects.hash(id, company, modelName);
}
}
航班时刻表
package org.learning.api.Flights.Entities;
import javax.persistence.*;
import java.util.Date;
import java.util.Objects;
@Entity(name="FlightSchedule")
public class FlightSchedule {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Airline airline;
private Flight flight;
private Airport source;
private Airport destination;
private Date scheduledDeparture;
private Date scheduledArrival;
public FlightSchedule() {
}
public FlightSchedule(Long id, Airline airline, Flight flight, Airport source, Airport destination, Date scheduledDeparture, Date scheduledArrival) {
this.id = id;
this.airline = airline;
this.flight = flight;
this.source = source;
this.destination = destination;
this.scheduledDeparture = scheduledDeparture;
this.scheduledArrival = scheduledArrival;
}
public FlightSchedule(Airline airline, Flight flight, Airport source, Airport destination, Date scheduledDeparture, Date scheduledArrival) {
this.airline = airline;
this.flight = flight;
this.source = source;
this.destination = destination;
this.scheduledDeparture = scheduledDeparture;
this.scheduledArrival = scheduledArrival;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Airline getAirline() {
return airline;
}
public void setAirline(Airline airline) {
this.airline = airline;
}
public Flight getFlight() {
return flight;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
public Airport getSource() {
return source;
}
public void setSource(Airport source) {
this.source = source;
}
public Airport getDestination() {
return destination;
}
public void setDestination(Airport destination) {
this.destination = destination;
}
public Date getScheduledDeparture() {
return scheduledDeparture;
}
public void setScheduledDeparture(Date scheduledDeparture) {
this.scheduledDeparture = scheduledDeparture;
}
public Date getScheduledArrival() {
return scheduledArrival;
}
public void setScheduledArrival(Date scheduledArrival) {
this.scheduledArrival = scheduledArrival;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FlightSchedule that = (FlightSchedule) o;
return Objects.equals(id, that.id) && Objects.equals(airline, that.airline) && Objects.equals(flight, that.flight) && Objects.equals(source, that.source) && Objects.equals(destination, that.destination) && Objects.equals(scheduledDeparture, that.scheduledDeparture) && Objects.equals(scheduledArrival, that.scheduledArrival);
}
@Override
public int hashCode() {
return Objects.hash(id, airline, flight, source, destination, scheduledDeparture, scheduledArrival);
}
@Override
public String toString() {
return "FlightSchedule{" +
"id=" + id +
", airline=" + airline +
", flight=" + flight +
", source=" + source +
", destination=" + destination +
", scheduledDeparture=" + scheduledDeparture +
", scheduledArrival=" + scheduledArrival +
'}';
}
}
将此作为答案发布:看起来您在 FlightSchedule.destination
、FlightSchedule.source
和 FlightSchedule.flight
.
上缺少@ManyToOne
更详细的回答。 Hibernate 需要知道如何 link a FlightSchedule
with an Airport
。目前,该代码没有任何注释来暗示此映射应该如何工作。它可以是任何潜在的映射类型。由于一个机场可以是多个航班时刻表的起点或终点,因此该关系需要是多对一的。
通过添加 @ManyToOne
你告诉休眠你想要 table flight_schedule
中的一列,外键指向 table airport
,这就是它稍后用来加入列并在您加载航班时刻表时检索相关机场的内容。同样适用于FlightSchedule.flight
我希望以上是有道理的!如果您想查看一些映射示例,可以查看 this repo. The hibernate docs are (usually) quite good: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#associations
启动我的 spring 引导项目时出现以下异常 - 原因:org.hibernate.MappingException:无法确定类型:org.learning.api.Flights.Entities.Airport,在 table:flight_schedule,对于列:[org.hibernate.mapping.Column(目标)]
正在使用的实体如下。
航空公司
package org.learning.api.Flights.Entities;
import javax.persistence.*;
import java.util.Objects;
import java.util.Set;
@Entity(name="Airline")
public class Airline {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade=CascadeType.ALL,mappedBy = "airline")
private Set<FlightSchedule> flightSchedules;
public Airline() {
}
public Airline(Long id, String name, Set<FlightSchedule> flightSchedules) {
this.id = id;
this.name = name;
this.flightSchedules = flightSchedules;
}
public Airline(String name, Set<FlightSchedule> flightSchedules) {
this.name = name;
this.flightSchedules = flightSchedules;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<FlightSchedule> getFlightSchedules() {
return flightSchedules;
}
public void setFlightSchedules(Set<FlightSchedule> flightSchedules) {
this.flightSchedules = flightSchedules;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Airline airline = (Airline) o;
return Objects.equals(id, airline.id) && Objects.equals(name, airline.name) && Objects.equals(flightSchedules, airline.flightSchedules);
}
@Override
public int hashCode() {
return Objects.hash(id, name, flightSchedules);
}
@Override
public String toString() {
return "Airline{" +
"id=" + id +
", name='" + name + '\'' +
", flightSchedules=" + flightSchedules +
'}';
}
}
机场
package org.learning.api.Flights.Entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;
@Entity(name="Airport")
public class Airport {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String city;
public Airport() {
}
public Airport(Long id, String name, String city) {
this.id = id;
this.name = name;
this.city = city;
}
public Airport(String name, String city) {
this.name = name;
this.city = city;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Airport airport = (Airport) o;
return Objects.equals(id, airport.id) && Objects.equals(name, airport.name) && Objects.equals(city, airport.city);
}
@Override
public int hashCode() {
return Objects.hash(id, name, city);
}
@Override
public String toString() {
return "Airport{" +
"id=" + id +
", name='" + name + '\'' +
", city='" + city + '\'' +
'}';
}
}
航班
package org.learning.api.Flights.Entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;
@Entity(name = "Flight")
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String company;
private String modelName;
public Flight(){}
public Flight(Long id, String company, String modelName) {
this.id = id;
this.company = company;
this.modelName = modelName;
}
public Flight(String company, String modelName) {
this.company = company;
this.modelName = modelName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
@Override
public String toString() {
return "Flight{" +
"id=" + id +
", company='" + company + '\'' +
", modelName='" + modelName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Flight flight = (Flight) o;
return Objects.equals(id, flight.id) && Objects.equals(company, flight.company) && Objects.equals(modelName, flight.modelName);
}
@Override
public int hashCode() {
return Objects.hash(id, company, modelName);
}
}
航班时刻表
package org.learning.api.Flights.Entities;
import javax.persistence.*;
import java.util.Date;
import java.util.Objects;
@Entity(name="FlightSchedule")
public class FlightSchedule {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Airline airline;
private Flight flight;
private Airport source;
private Airport destination;
private Date scheduledDeparture;
private Date scheduledArrival;
public FlightSchedule() {
}
public FlightSchedule(Long id, Airline airline, Flight flight, Airport source, Airport destination, Date scheduledDeparture, Date scheduledArrival) {
this.id = id;
this.airline = airline;
this.flight = flight;
this.source = source;
this.destination = destination;
this.scheduledDeparture = scheduledDeparture;
this.scheduledArrival = scheduledArrival;
}
public FlightSchedule(Airline airline, Flight flight, Airport source, Airport destination, Date scheduledDeparture, Date scheduledArrival) {
this.airline = airline;
this.flight = flight;
this.source = source;
this.destination = destination;
this.scheduledDeparture = scheduledDeparture;
this.scheduledArrival = scheduledArrival;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Airline getAirline() {
return airline;
}
public void setAirline(Airline airline) {
this.airline = airline;
}
public Flight getFlight() {
return flight;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
public Airport getSource() {
return source;
}
public void setSource(Airport source) {
this.source = source;
}
public Airport getDestination() {
return destination;
}
public void setDestination(Airport destination) {
this.destination = destination;
}
public Date getScheduledDeparture() {
return scheduledDeparture;
}
public void setScheduledDeparture(Date scheduledDeparture) {
this.scheduledDeparture = scheduledDeparture;
}
public Date getScheduledArrival() {
return scheduledArrival;
}
public void setScheduledArrival(Date scheduledArrival) {
this.scheduledArrival = scheduledArrival;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FlightSchedule that = (FlightSchedule) o;
return Objects.equals(id, that.id) && Objects.equals(airline, that.airline) && Objects.equals(flight, that.flight) && Objects.equals(source, that.source) && Objects.equals(destination, that.destination) && Objects.equals(scheduledDeparture, that.scheduledDeparture) && Objects.equals(scheduledArrival, that.scheduledArrival);
}
@Override
public int hashCode() {
return Objects.hash(id, airline, flight, source, destination, scheduledDeparture, scheduledArrival);
}
@Override
public String toString() {
return "FlightSchedule{" +
"id=" + id +
", airline=" + airline +
", flight=" + flight +
", source=" + source +
", destination=" + destination +
", scheduledDeparture=" + scheduledDeparture +
", scheduledArrival=" + scheduledArrival +
'}';
}
}
将此作为答案发布:看起来您在 FlightSchedule.destination
、FlightSchedule.source
和 FlightSchedule.flight
.
更详细的回答。 Hibernate 需要知道如何 link a FlightSchedule
with an Airport
。目前,该代码没有任何注释来暗示此映射应该如何工作。它可以是任何潜在的映射类型。由于一个机场可以是多个航班时刻表的起点或终点,因此该关系需要是多对一的。
通过添加 @ManyToOne
你告诉休眠你想要 table flight_schedule
中的一列,外键指向 table airport
,这就是它稍后用来加入列并在您加载航班时刻表时检索相关机场的内容。同样适用于FlightSchedule.flight
我希望以上是有道理的!如果您想查看一些映射示例,可以查看 this repo. The hibernate docs are (usually) quite good: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#associations