-->
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.  [ 2 posts ] 
Author Message
 Post subject: OneToMany Problems with Delete, Merge the Childs
PostPosted: Fri Jul 13, 2007 10:14 am 
Newbie

Joined: Fri Jul 06, 2007 9:07 am
Posts: 5
Hibernate version:
3.2
Mapping documents:
Annotations
Name and version of the database you are using:
MySQL 5.0

Hi,

I have some problem with a OneToMany Relation. I have a class called "Event" which has many "Action" objects. The Action objects were saved in an ArrayList inside the Event.

The first time I started the program, everything is right all table were created and all data were stored. If I restarted the program I checked if the event object is allready in the DB and than merge the new one with the one in the DB. That is because the events came from files and every start the files must be refreshed to have a consistent content.

After the restart the Event is refreshed and the linked Actions became a new entry. So every restart the number ob Actions grow, and I have many unlinked Actions in the DB.
If I delete the Event the Actions aren't deleted too.

What can I do to delete the childs (Actions) when the parent (Event) is deleted and when the parent with his childes are merged that the childes become no new entries?
Please help!

add() Method from AutoCalMonitor.class:
Code:
private void add() {
      String query = null;
      Session session = HibernateHandler.getSessionFactory().getCurrentSession();
      session.beginTransaction();
   
      // Prüfen ob Plugin schon vorhanden
      query = "Select id from Event where fileName='" + fileName + "'";
      Integer eventID = (Integer) session.createQuery(query).uniqueResult();
         
      // Object für die Datenbank vorbereiten
      Event eventObject = parse();
      eventObject.setScanDate(scanDate);
      
      // Beispielhaftes Hinzufügen einer Aktion
      Action display = new Action();      
      display.setOffset(10);
      display.setTest(77);
      eventObject.addAction(display);      
      
      if(eventID == null) {
           session.save(eventObject);
      } else {
            eventObject.setEventID(eventID);
            session.merge(eventObject);
      }
         
      session.getTransaction().commit();
      
      return;
   }


remove() Method from AutoCalMonitor.class:
Code:
rivate void remove() {
      String query = null;
      Session session = HibernateHandler.getSessionFactory().getCurrentSession();
      session.beginTransaction();

      query = "Delete from Event as Event where scanDate!='" + scanDate + "'";
       session.createQuery(query).executeUpdate();      
      
       session.getTransaction().commit();
      return;
   }


Event.class:
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Event implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int eventID = 0;

   @Column(nullable=false)
   private String firstStart = null;

   @Column(nullable=false)
   private String firstEnd = null;
   
   @Column
   private String repeatFreq = null;
   
   @Column(nullable=false)
   private long scanDate = 0;

   @OneToMany(cascade=CascadeType.ALL)
   private List<Action> lnkAction = new ArrayList<Action>();
   ...


Action.class:
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Action implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int actionID = 0;
   
   @Column(nullable=false)
   private String triggerPulse = null;
   
   @Column(nullable=false)
   private int offset = 0;
...


Top
 Profile  
 
 Post subject: fetching
PostPosted: Tue Jul 17, 2007 8:16 am 
Newbie

Joined: Thu May 10, 2007 4:19 am
Posts: 5
What type of lazy fetching do You use in xml mapping file? Try to use lazy=false, or if can't use lazy-false when You load the parent:

//Your criteria for parent class:
................................
criteria.setFetchMode("childsArray", FetchMode.JOIN);


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

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.