-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping v. Beziehungen und was Hibernate daraus macht
PostPosted: Wed Oct 24, 2007 4:59 am 
Newbie

Joined: Thu May 18, 2006 7:38 am
Posts: 10
Hallo!

Ich habe 3 Klassen (Domain-Model) mittels Hibernate realisiert. Es funktioniert alles tadellos, aber ich habe immer ein Auge darauf welche SQL-Statements Hibernate erzeugt. Jetzt habe ich eine Situation wo es nicht verstehe.

Klasse: Person
Code:
@Entity
public class Person extends PersistentObjectAbstract {
   ...
   @Column(length=100, unique=false, nullable=true)
   private String firstName = null;

   @Column(length=100, unique=false, nullable=true)
   private String lastName = null;

   @OneToMany(fetch=FetchType.LAZY)
   @JoinColumn(name="personId")
   @IndexColumn(name="listPos")
   @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
   private List<Communication> communications = new ArrayList<Communication>();   ...


Klasse: Organization
Code:
@Entity
public class Organization extends PersistentObjectAbstract {
   ...
   @Column(length=200, unique=false, nullable=false)
   private String longName = null;

   @OneToMany(fetch=FetchType.LAZY)
   @JoinColumn(name="organizationId")
   @IndexColumn(name="listPos")
   @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
   private List<Communication> communications = new ArrayList<Communication>();
   ...


Klasse: Communication
Code:
@Entity
public class Communication extends PersistentObjectAbstract {
   ...
   @Column(nullable=true)
   private int listPos = 0;

   @Column(length=100, unique=false, nullable=true)
   private String label = null;

   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumn(name="personId", insertable=false, updatable=false, nullable=true)
   private Person person = null;

   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumn(name="organizationId", insertable=false, updatable=false, nullable=true)
   private Organization organization = null;
   ...


Navigation erfolgt über Person -> Communication bzw. Organization -> Communication

Wenn ich nun eine Person lösche macht Hibernate folgendes:
Code:
Hibernate: update Communication set personId=null, listPos=null where personId=?
Hibernate: delete from Communication where id=? and version=?
Hibernate: delete from Communication where id=? and version=?
Hibernate: delete from Person where id=? and version=?


Erwarten würde ich aber (weil performanter):
Code:
Hibernate: delete from Communication where personid=?
Hibernate: delete from Person where id=? and version=?


Ist das so? Oder habe ich einen Fehler in meinem Mapping?
Marco[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 8:58 am 
Newbie

Joined: Thu May 18, 2006 7:38 am
Posts: 10
Ich dürfte wirklich einen Fehler in meinem Mapping haben.

Wenn ich folgendes mache

Code:
Person person = (Person) session.load // Person laden
person.getCommunications().remove(0);
session.update(person);


erhalte ich

Code:
org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [at.brisk.jazz.domain.Person.communications


Somit bin ich wieder am Anfang :-(

Hat jemand einen Tipp für mich?
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 03, 2007 4:27 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
nach session.load sollte Dein Objekt bereits persistent sein. Das session.update wäre damit unnötig. Was passiert wenn Du das weglässt?

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.