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;
...