是否存在控制持久性提供程序创建表的顺序的方法?我得到了这个mysql 1146错误。我想这是因为它试图创建一个需要保留表的实体,但它没有找到它,所以这会导致以下异常。有没有办法解决这个问题?
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'volaconnoi_db.reservation' doesn't exist
Error Code: 1146
Call: ALTER TABLE RESERVATION ADD CONSTRAINT FK_RESERVATION_ROUTE_ID_ROUTE FOREIGN KEY (ROUTE_ID_ROUTE) REFERENCES ROUTE (ID_ROUTE)
Query: DataModifyQuery(sql="ALTER TABLE RESERVATION ADD CONSTRAINT FK_RESERVATION_ROUTE_ID_ROUTE FOREIGN KEY (ROUTE_ID_ROUTE) REFERENCES ROUTE (ID_ROUTE)")
这是USER_CREDENTIAL实体
@Entity
@Table(name = "USER_CREDENTIAL")
@SecondaryTable(name = "CLIENT", pkJoinColumns=@PrimaryKeyJoinColumn(name="USERNAME"))
public class UserCredential implements Serializable
{
private String username;
private String password;
private String email;
private String group_name;
private Date create_date;
private String name;
private String surname;
private String address;
private String city;
private String zip_code;
private String country;
private int fidelity_points;
private List<PhoneNumber> phoneNumbers;
private List<Reservation> reservationsList;
public UserCredential()
{
}
@Id
@Column(name = "USERNAME", nullable = false)
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
@Column(name = "PASSWORD", nullable = false)
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
@Column(name = "EMAIL", nullable = false)
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
@Column(name = "GROUP_NAME", insertable = false, updatable = false)
public String getGroup_name()
{
return group_name;
}
public void setGroup_name(String group_name)
{
this.group_name = group_name;
}
@Column(name = "CREATE_DATE", insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
public Date getCreate_date()
{
return create_date;
}
public void setCreate_date(Date create_date)
{
this.create_date = create_date;
}
@Column(name = "NAME", nullable= false, table="CLIENT")
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Column(name = "SURNAME", nullable= false, table = "CLIENT")
public String getSurname()
{
return surname;
}
public void setSurname(String surname)
{
this.surname = surname;
}
@Column(name = "ADDRESS", nullable= false , table = "CLIENT")
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Column(name = "CITY", nullable = false, table = "CLIENT")
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
@Column(name = "ZIP_CODE", nullable = false, table = "CLIENT")
public String getZip_code()
{
return zip_code;
}
public void setZip_code(String zip_code)
{
this.zip_code = zip_code;
}
@Column(name = "COUNTRY", nullable = false, table = "CLIENT")
public String getCountry()
{
return country;
}
public void setCountry(String country)
{
this.country = country;
}
@Column(name = "FIDELITY_POINTS", nullable = false, table = "CLIENT")
public int getFidelity_points()
{
return fidelity_points;
}
public void setFidelity_points(int fidelity_points)
{
this.fidelity_points = fidelity_points;
}
@ElementCollection
@CollectionTable(name = "CLIENT_PHONE_NUMBER", joinColumns = @JoinColumn(name = "USERNAME"))
public List<PhoneNumber> getPhoneNumbers()
{
return phoneNumbers;
}
public void setPhoneNumbers (List<PhoneNumber> phoneNumbers)
{
this.phoneNumbers = phoneNumbers;
}
@OneToMany(mappedBy = "username", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public List<Reservation> getReservationsList()
{
return reservationsList;
}
public void setReservationsList(List<Reservation> reservationsList)
{
this.reservationsList = reservationsList;
}
@Override
public int hashCode()
{
int hash = 0;
hash += (username != null ? username.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object)
{
// TODO: Warning - this method won't work in the case the username fields are not set
if (!(object instanceof UserCredential))
{
return false;
}
UserCredential other = (UserCredential) object;
if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username)))
{
return false;
}
return true;
}
@Override
public String toString()
{
return "it.volaconoi.entity.UserCredential[ id=" + username + " ]";
}
}
这是路线实体
@Entity
@Table(name = "ROUTE")
public class Route implements Serializable
{
private String id_route;
private String airlane;
private String aircraft_id;
private Airport airport_city_source;
private Airport airport_city_dest;
private Date departure_date;
private Date arrival_date;
private String travel_class;
private int seats;
private float price;
private List<Reservation> reservationsList;
public Route()
{
}
@PrePersist
public void setIdRoute()
{
SimpleDateFormat sdf = new SimpleDateFormat("ddMMYYYYHHmm");
String format_departure_date = sdf.format(this.getDeparture_date());
String unique_id_route = this.getAirlane() +
this.getAircraft_id() +
this.getAirport_city_source().getCity() +
this.getAirport_city_dest().getCity() +
format_departure_date;
this.setId_route(unique_id_route.replaceAll(" ", ""));
}
@Id
@Column(name = "ID_ROUTE")
public String getId_route()
{
return id_route;
}
public void setId_route(String id_route)
{
this.id_route = id_route;
}
@Column(name = "AIRLANE", nullable = false)
public String getAirlane()
{
return airlane;
}
public void setAirlane(String airlane)
{
this.airlane = airlane;
}
@Column(name = "AIRCRAFT_ID", nullable = false)
public String getAircraft_id()
{
return aircraft_id;
}
public void setAircraft_id(String aircraft_id)
{
this.aircraft_id = aircraft_id;
}
@OneToOne(optional = false)
public Airport getAirport_city_source()
{
return airport_city_source;
}
public void setAirport_city_source(Airport airport_city_source)
{
this.airport_city_source = airport_city_source;
}
@OneToOne(optional = false)
public Airport getAirport_city_dest()
{
return airport_city_dest;
}
public void setAirport_city_dest(Airport airport_city_dest)
{
this.airport_city_dest = airport_city_dest;
}
@Column(name = "DEPARTURE_DATE", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
public Date getDeparture_date()
{
return this.departure_date;
}
public void setDeparture_date(Date departure_date)
{
this.departure_date = departure_date;
}
@Column(name = "ARRIVAL_DATE", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
public Date getArrival_date()
{
return arrival_date;
}
public void setArrival_date(Date arrival_date)
{
this.arrival_date = arrival_date;
}
@Column(name = "TRAVEL_CLASS", nullable = false)
public String getTravel_class()
{
return travel_class;
}
public void setTravel_class(String travel_class)
{
this.travel_class = travel_class;
}
@Column(name = "SEATS", nullable = false)
public int getSeats()
{
return seats;
}
public void setSeats(int seats)
{
this.seats = seats;
}
@Column(name = "PRICE", nullable = false)
public float getPrice()
{
return price;
}
public void setPrice(float price)
{
this.price = price;
}
@OneToMany(mappedBy = "route", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public List<Reservation> getReservationsList()
{
return reservationsList;
}
public void setReservationsList(List<Reservation> reservationsList)
{
this.reservationsList = reservationsList;
}
@Override
public int hashCode()
{
int hash = 0;
hash += (id_route != null ? id_route.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object)
{
// TODO: Warning - this method won't work in the case the id_route fields are not set
if (!(object instanceof Route))
{
return false;
}
Route other = (Route) object;
if ((this.id_route == null && other.id_route != null) || (this.id_route != null && !this.id_route.equals(other.id_route)))
{
return false;
}
return true;
}
@Override
public String toString()
{
return "it.volaconoi.entity.Route[ id=" + id_route + " ]";
}
}
这是RESERVATION实体
@Entity
@Table(name = "RESERVATION")
public class Reservation implements Serializable
{
private String id;
private int passengers;
private int luggages;
private float price;
private Date date_reservation;
private boolean cancelled;
private UserCredential username;
private Route route;
public Reservation()
{
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_RESERVATION")
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
@Column(name = "PASSENGERS", nullable = false)
public int getPassengers()
{
return passengers;
}
public void setPassengers(int passengers)
{
this.passengers = passengers;
}
@Column(name = "LUGGAGES", nullable = false)
public int getLuggages()
{
return luggages;
}
public void setLuggages(int luggages)
{
this.luggages = luggages;
}
@Column(name = "PRICE", nullable = false)
public float getPrice()
{
return price;
}
public void setPrice(float price)
{
this.price = price;
}
@Column(name = "DATE_PLACED", insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
public Date getDate_reservation()
{
return date_reservation;
}
public void setDate_reservation(Date date_reservation)
{
this.date_reservation = date_reservation;
}
@Column(name = "CANCELLED", nullable = false)
public boolean isCancelled()
{
return cancelled;
}
public void setCancelled(boolean cancelled)
{
this.cancelled = cancelled;
}
@ManyToOne
@JoinColumn(name = "USERNAME", nullable = false)
public UserCredential getUsername()
{
return username;
}
public void setUsername(UserCredential username)
{
this.username = username;
}
@ManyToOne
@JoinColumn(name = "ID_ROUTE", nullable = false)
public Route getRoute()
{
return route;
}
public void setRoute(Route route)
{
this.route = route;
}
@Override
public int hashCode()
{
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object)
{
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Reservation))
{
return false;
}
Reservation other = (Reservation) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
{
return false;
}
return true;
}
@Override
public String toString()
{
return "it.volaconoi.entity.Reservation[ id=" + id + " ]";
}
}
您可能会看到这三个实体与每个实体相关
答案 0 :(得分:2)
我认为这是一个错误,所以解决这个问题可能会让你的问题消失。然后,这可能是无关的:
在Reservation
中,id
的类型为String
,但在getId()
中您指定了GenerationType.IDENTITY
。 AFAIK MySQL不支持自动生成字符串ID,但只支持整数ID。删除它,看看是否有效。
<强>更新强>
我在我的机器上重现了错误,这确实是问题所在。如果检查输出,您会发现类似于:
的警告(不是错误)[EL警告]:2014-06-20 15:47:46.224 - ServerSession(1565614310) - 异常[EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException内部 例外:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 列&#39; ID&#39;列的列说明符不正确错误代码:1063致电: CREATE TABLE NEWENTITY(ID VARCHAR(255)AUTO_INCREMENT NOT NULL,NAME VARCHAR(255),PRIMARY KEY(ID))查询:DataModifyQuery(sql =&#34; CREATE 表NEWENTITY(ID VARCHAR(255)AUTO_INCREMENT NOT NULL,NAME VARCHAR(255),PRIMARY KEY(ID))&#34;)
我想你错过了它,因为它是一个警告,而不是一个错误。我还想象这会将输出作为警告而不是错误,因为有时创建表的错误不是问题(例如,如果表已经存在)。 EclipseLink显然不够聪明,无法处理存在真实错误的情况,因此它会作为警告输出(参见&#34; JPA糟透了,#34;,上面)。
EclipseLink / MySQL组合不支持字符串ID的生成类型IDENTITY
。 IDENTITY
表示由数据库(而不是JPA实现提供程序)创建ID。 MySQL仅支持创建整数ID,因此如果使用AUTO INCREMENT
,则列类型必须为整数(请参阅生成的代码)。
如果您确实希望自己的ID是字符串,但也自动生成ID,则使用AUTO
的生成类型。 AUTO
表示JPA实现提供程序将处理创建ID; EclipseLink将使用序列表,并将处理将值转换为String。