Hibernate: Mapping v. Beziehungen und was Hibernate daraus macht

DerGrinsemann

Mitglied
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
 
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
 

Neue Beiträge

Zurück