-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate - Probleme save eines Objekts mit Abhängigkeiten
PostPosted: Tue Feb 05, 2008 4:09 am 
Newbie

Joined: Fri May 25, 2007 4:03 am
Posts: 15
Hibernate version:
3.2.5.ga
Mapping documents:
Receiver:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.pcd.wam.gegenstandsbereich.streetDetection.persistence.model">
  <class name="org.pcd.wam.gegenstandsbereich.streetDetection.domain.model.locating.valueObject.Receiver"
        table="RECEIVER">          
      <!-- primary key -->
      <id name="id" type="integer" column="RE_ID">
      <generator class="increment" />
      </id>      
      <property name="globalID" type="string" column="RE_GLOBAL_ID" />
      <property name="description" type="string" column="RE_DESC" />
   </class>
</hibernate-mapping>

Track:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.pcd.wam.gegenstandsbereich.streetDetection.persistence.model">
  <class name="org.pcd.wam.gegenstandsbereich.streetDetection.domain.model.locating.valueObject.Track"
        table="TRACK">        
              <!-- primary key -->
      <id name="id" type="integer" column="T_ID">
      <generator class="increment" />
      </id>      
      <many-to-one name="receiver" class="org.pcd.wam.gegenstandsbereich.streetDetection.domain.model.locating.valueObject.Receiver"
            column="T_RE_ID" lazy="false" cascade=""/>      
      <property name="end" type="timestamp" column="T_END" />
      <property name="cmt" type="string" column="T_GPS_CMT" />
      <property name="start" type="timestamp" column="T_START" />
      <property name="desc" type="string" column="T_USER_DESC" />        
  </class> 
</hibernate-mapping>

TrackPoint:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.pcd.wam.gegenstandsbereich.streetDetection.persistence.model">
  <class name="org.pcd.wam.gegenstandsbereich.streetDetection.domain.model.locating.valueObject.TrackPoint"
        table="TRACK_POINT">        
              <!-- primary key -->
      <id name="id" type="integer" column="TP_ID">
      <generator class="increment" />
      </id>         
      <many-to-one name="track" class="org.pcd.wam.gegenstandsbereich.streetDetection.domain.model.locating.valueObject.Track"
            column="TP_T_ID" lazy="false"/>   
      <many-to-one name="trackEvent"
            class="org.pcd.wam.gegenstandsbereich.streetDetection.domain.model.event.valueObject.EventType"
            column="TP_TE_ID" lazy="false"/>            
      <property name="date" type="timestamp" column="TP_TIME" />
      <property name="ele" type="float" column="TP_ELE" />
      <property name="geoidHeight" type="float" column="TP_GEOID_HEIGHT" />
      <property name="hdop" type="float" column="TP_HDOP" />
      <property name="latitude" type="double" column="TP_LATITUDE" />
      <property name="longitude" type="double" column="TP_LONGITUDE" />
      <property name="pdop" type="float" column="TP_PDOP" />
      <property name="satelliteCount" type="integer" column="TP_SAT" />
      <property name="vdop" type="float" column="TP_VDOP" />   
  </class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
   public void storeTrack(final Track track) {
      Session session = HibernateUtil.getCurrentSession();
      TrackDAO trackDAO = new TrackDAO();
      TrackPointDAO trackPointDAO = new TrackPointDAO();
      ReceiverDAO receiverDAO = new ReceiverDAO();
      try {   
         session.beginTransaction();
         trackDAO.setSession(session);
         trackPointDAO.setSession(session);
         receiverDAO.setSession(session);
         if (!session.contains(track.getReceiver())) {
            Receiver receiver = receiverDAO.getReceiver(track.getReceiver().getId());
            if (receiver != null) {
               receiver = receiverDAO.makePersistent(receiver);
               track.setReceiver(receiver);
            } else {
               //FIXME welche Exception werfen!!
               throw new RuntimeException ("receiver which global id: " + receiver.getGlobalID()
                     + "doesnt exist");
            }
         }
         
         /* save or update track */
         trackDAO.makePersistent(track);
         
         for (TrackPoint point : track.getTrackPointList()) {
            point.setTrack(track);
            /* save or update track point */
            trackPointDAO.makePersistent(point);   
            break;
         }
         session.getTransaction().commit();   
      } catch (Exception e) {
         e.printStackTrace();
         if (session.getTransaction() != null) {
            session.getTransaction().rollback();
            TrackControllerModel.LOGGER.fatal(
                  "class: TrackControllerModel method: storeTrack, exception occurred while"
                  + " store tracks: ", e);
         }
      } finally {
         session = null;
         trackDAO = null;
         trackPointDAO = null;
         receiverDAO = null;
      }
   }

Full stack trace of any exception that occurs:
Code:
05.02.2008 09:00:55 at.pcd.wam.technologie.utility.hibernate.HibernateUtil getSessionFactory
INFO: initialize hibernate session factory, class: HibernateUtil method: getSessionFactory
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at org.pcd.wam.gegenstandsbereich.streetDetection.domain.model.locating.TrackControllerModel.storeTrack(TrackControllerModel.java:411)
   at org.pcd.wam.gegenstandsbereich.streetDetection.domain.model.locating.TrackControllerModel.storeTracks(TrackControllerModel.java:436)
   at org.pcd.wam.test.TestTrackControllerModel.main(TestTrackControllerModel.java:61)
Caused by: java.sql.BatchUpdateException: ORA-02291: Integritäts-Constraint (NAVTEQ.FK_TP_T_ID) verletzt - übergeordneter Schlüssel nicht gefunden

   at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
   at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10656)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
   ... 10 more

Name and version of the database you are using:
Oracle XE

Frage 1:
Also bei der Methode storeTrack(..) möchte ich einen Track in die DB schreiben, welcher einen Receiver besitzt (der schon in der DB vorhanden ist) und aus vielen TrackPoint Objekten besteht, welche wiederum einen FK des Tracks besitzen.

Also warum bekomme ich hier diese Exception, zumal ich ja die korrekte Reihenfolge der Inserts einhalte, sprich zuerst Track inserte und dann erst TrackPoint - also müsste der FK doch vorhanden und gültig sein. Oder muss ich hier die Session schließen und wieder öffnen (nachdem ich Track geinsertet habe)?

Frage 2:
Wie im Code oben ersichtlich gibt es ebenfalls Probleme mit dem bereits in der DB vorhandenen Receiver, denn jener ist der Session nicht bekannt und wenn ich ihn nicht bekannt mache, bekomme ich ebenfalls eine Exception - welche Möglichkeiten habe ich hier um das Receiver Objekt der session "bekannt" zu machen (nur save? - wird hier wirklich auch ein update gegen DB gemacht obwohl sich das Objekt nicht geändert hat?)?

mfg


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.