摘要:
NewRide类具有Route类的实例。 Route类具有SerializationLatLng类对象的集合。我正在尝试将NewRide对象持久化到数据库中,并期望在持久化NewRide的同时,Route和SerializationLatLng也会由于级联而持久化。
NewRide.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="edu.nyu.cloud.beans.NewRide" table="ride" entity-name="ride">
<id name="id" column="id" />
<property name="requester" column="USER_NAME" type="java.lang.String" />
<property name="source" column="source" type="java.lang.String" />
<property name="destination" column="destination" type="java.lang.String" />
<property name="timeOfTrip" column="timeOfTrip" type="java.sql.Date" />
<many-to-one name="selectedRoute" column="r_id"
cascade="persist" fetch="join" entity-name="route" not-null="true">
</many-to-one>
</class>
<class name="edu.nyu.cloud.beans.Route" table="route"
entity-name="route">
<id name="id" type="long" column="r_id" />
<component name="distance" class="edu.nyu.cloud.beans.SerializableDistance">
<property name="inMeters" column="distance" type="long" />
</component>
<component name="timetaken" class="edu.nyu.cloud.beans.SerializableDuration">
<property name="inSeconds" column="duration" type="long" />
</component>
<set name="latlng" inverse="true" cascade="persist" fetch="join" table="latlng">
<key column="r_id" not-null="true"/>
<one-to-many class="edu.nyu.cloud.beans.SerializableLatLng"
entity-name="latlng" />
</set>
</class>
<class name="edu.nyu.cloud.beans.SerializableLatLng" entity-name="latlng">
<id name="id" type="long" column="id">
<generator class="identity" />
</id>
<property name="lat" type="double" column="lat" />
<property name="lng" type="double" column="lng" />
<property name="routeId" type="long" column = "r_id" not-null="true"/>
</class>
</hibernate-mapping>
NewRide.java
public class NewRide implements Serializable {
private static final long serialVersionUID = 1L;
private long id;
private String requester;
private String source;
private String destination;
private Date timeOfTrip;
private Route selectedRoute;
public NewRide(String requester, String source, String destination, Date timeOfTrip, Route selectedRoute) {
super();
this.requester = requester;
this.source = source;
this.destination = destination;
this.timeOfTrip = timeOfTrip;
this.selectedRoute = selectedRoute;
}
Route.java
public class Route implements Serializable {
private static final long serialVersionUID = 3571130958082192755L;
private Long id;
private List<String> address;
private SerializableDistance distance;
private SerializableDuration timetaken;
private Set<SerializableLatLng> latlng;
public Route(Long id, List<String> address, SerializableDistance distance, SerializableDuration timetaken,
Set<SerializableLatLng> latlng) {
super();
this.id = id;
this.address = address;
this.distance = distance;
this.timetaken = timetaken;
this.latlng = latlng;
}
SerializableLatLng.java
public class SerializableLatLng implements Serializable {
private static final long serialVersionUID = 3933584737903911424L;
private long id;
/**
* The latitude of this location.
*/
private double lat;
/**
* The longitude of this location.
*/
private double lng;
@JsonIgnore
private long routeId;
造成原因:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`rideshare`.`latlng`, CONSTRAINT `FK_chp98lqa8608o464bhs6qpoyk` FOREIGN KEY (`r_id`) REFERENCES `route` (`r_id`))at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2643)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 104 common frames omitted
最佳答案
请检查
1.您要在latlng表中插入的r_id值应与路由表中的r_id值相同。
2.路由表应该在更新表之前更新。