-->
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.  [ 5 posts ] 
Author Message
 Post subject: Object-Deletion in an one-to-one association
PostPosted: Mon Aug 07, 2006 4:59 pm 
Newbie

Joined: Mon Jul 31, 2006 10:57 am
Posts: 8
Hi,

I'm new with hibernate and have a question about deletion in a one-to-one association.

I have a class Person that has one-to-one association to the company the person works for.

@Entity
@AccessType(value = "property")
public class Person extends PersistentObject {

protected Company company;

@OneToOne(cascade = CascadeType.ALL)
public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}
...
}

@Entity
public class Company extends PersistentObject {
private String name;

public Company() {
}

//Getter + Setter
}

A person gets its company by
person.setCompany(new Company())

I want to delete the person's company in that way

person.setCompany(null)

if I do an update of the person

session.update(Person)

the foreign key column for referencing to the company in the person table is emptied by the CascadeType.All statement but the company still exists in the company table. So I have a lot of dead objects in the database which can't get referencend any more.

I tried the orphan Deletion...
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
...but, like the hibernate doco says, that works only for one-to-many associations (why?).

So are there any other solutions to get the company completly deleted in the database after the person lost its reference on it? (Without losing the single tables for the persons and the companys)

Thanks a lot for help!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 10:31 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Use setCompany(null) in order to not delete the company (as you have discovered). Use session.delete(person) to delete the company: your cascade takes care of that.

I have to ask: why one-to-one? That doesn't look right. Shouldn't it be one-to-many from company to person? And many-to-one from person to company.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 4:33 am 
Newbie

Joined: Mon Jul 31, 2006 10:57 am
Posts: 8
Currently I'm using a one-to-one association because the company is not really important in the application.
There should just be the possibility to store for every person the information for which he/she works for. The information that two different persons work for the same company is not needed.
But you are right, perhaps it is better to do it as an many-to-one association, I will think about it...

But the problem is not solved because like the company the person have a one-to-one Association to it's BankAccount. I think that is an one-to-one association?!

if I don't want to delete the whole person with session.delete(person) an my person got a BankAccount with
BankAccount account = new BankAccount();
account.setName("Blah")
person.setBankAccount(account)
If I now want to delete the bank account, what is to do?
Do I need something like a ServiceClass which does that for me? That would not be so nice because for other person things like emailaddresses, phonenumbers,... which are in one-to-many associations I do not need a ServiceClass because of
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
I just have to remove it from the collection and update the person...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 5:20 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
To delete something on the one-end of a many-to-one or one-to-one, you can either cascade delete from the "main" end (delete a person, and the bank account is deleted), or delete the entity directly (session.delete(person.getBankAccount());). If you choose the second option, you obviously have to set the bank account in the person to null at the same time.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 6:30 pm 
Newbie

Joined: Mon Jul 31, 2006 10:57 am
Posts: 8
Ok, thanks for your help!
I will realise it somehow in the second way.


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