簡體   English   中英

一對多層次結構對象結構在使用hibernate時沒有持久化

[英]One-to-many hierarchy object structure not getting persisted on using hibernate

摘要:

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.路由表應在latlng表之前更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM