-->
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.  [ 6 posts ] 
Author Message
 Post subject: @NotFound annotation only solution to optional associations?
PostPosted: Sun Apr 15, 2007 2:45 pm 
Newbie

Joined: Sun Apr 15, 2007 2:29 pm
Posts: 7
Location: Montpellier
Hi all,

I have a ManyToOne unidirectional association . This association is optional meaning that the foreign key value may be 0. If if use the following annotations, Hibernate throws an ObjectNotFoundException :

@ManyToOne(optional = true) // doesn't work
@JoinColumn(name = "Contrat_idContrat", nullable = true) // doesn't work

The only annotation that avoids hibernate to throw he exception is the @NotFound annotation :

@ManyToOne
@JoinColumn(name = "Contrat_idContrat")
@NotFound(action = NotFoundAction.IGNORE)

BUT, HIBERNATE STILL MAKE THE SQL QUERY WHICH IS QUITE A PERFORMANCE PROBLEM BECAUSE I HAVE A LOT OF ENTITES -> LOTS OF REQUESTS FOR AN OBJECT THAT WILL NEVER BE FOUND.

Is the @NotFound annotation the only solution to optional associations ?

Thanks in advance,

Sylvain

Hibernate version:
3.2.2.ga (+annotations 3.2.1.ga)
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Mysql InnoDB 4.0.20

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 3:31 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
I guess that si tu postes dans le forum Hibernate french, tu aurais pu le dire en french :-).

Premièrement, la chose qui me surprend dans ton cas, c'est que tu dis que la colonne qui référence la clé étrangère peut valoir 0. Je suppose que tu veux dire null ? Sinon, c'est normal que tu sois obligé de contourner ce pb avec @NotFound : 0 ne veut pas dire pas de référence, ça veut dire que ça référence le tuple qui a 0 en clé primaire, c'est tout.

Une association 0..1, puisque c'est de ça que tu parles il me semble, soit la colonne contient une valeur qui est donc une clé étrangère => Hibernate va lancer un select pour y accéder si tu le demandes. Soit c'est null, et ça renverra null au niveau Java sans aucun pb. Aussi simple que ça :-)

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 2:24 am 
Newbie

Joined: Sun Apr 15, 2007 2:29 pm
Posts: 7
Location: Montpellier
batmat wrote:
I guess that si tu postes dans le forum Hibernate french, tu aurais pu le dire en french :-).


Your guess est correct mais quand j'ai la tête dans le c... je fais n'importe quoi ... (la preuve).

Merci de m'avoir éclairé. Je disposais d'une base de données existante et la valeur 0 de clé étrangère avait été positionnée par un vieux layer relationnel-objet. Après avoir transformé toutes les valeurs 0 en null en base, hibernate ne va plus essayer de résoudre la relation.


Si je récapitule :

- dans un fonctionnement normal, les clé étrangères doivent avoir la valeur NULL et non 0 s'il n'y a pas d'objet lié;

- si on a une base existante que l'on ne peut modifier, on utilise l'annotation @NotFound mais ce n'est pas très performant puisque hibernate tentera à chaque fois de la résoudre.

Merci à toi,

Sylvain


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 2:30 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Oui, 0 n'est pas la même chose que la valeur null pour une colonne. Pour représenter du 0..X (que X vaille 1 ou n, ou n'importe quoi d'autre), on utilisera null pour le 0 (pas de relation).

Le 0 n'a pas du tout la même sémantique : qu'est-ce qui dit que le 0 en question indique qu'il n'y a pas de relation plutôt que le fait que ça pointe vers une clé qui vaut "0" ?

Donc, oui. @NotFound n'est pas à utiliser de base. Mais dans les cas de bases héritées/vieilles qui ont des clés qui ne référencent pas forcément une ligne existante, c'est la seule solution.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 19, 2007 11:57 pm 
Newbie

Joined: Thu Apr 19, 2007 11:47 pm
Posts: 2
Sorry the english... I don't speak french.

I have the same problem posted here by liop.
Unfortunately, I could not understand what was written.

My question: Is the @NotFound annotation the only solution to optional associations ?

Thanks,

TCP


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 5:19 am 
Newbie

Joined: Sun Apr 15, 2007 2:29 pm
Posts: 7
Location: Montpellier
tiagopeixoto wrote:
Sorry the english... I don't speak french.

I have the same problem posted here by liop.
Unfortunately, I could not understand what was written.

My question: Is the @NotFound annotation the only solution to optional associations ?

Thanks,

TCP


Well,

translated in english, what I wrote was that NULL is the normal value that you should find in the database for an foreign key that does not point to a foreign record.

So if you're working on a brand new project with brand new data, NULL is the way to go.
BUT if you're working with legacy data, you're only option is to use the @NotFound annotation if you don't consider modifying all your old 'foreign key 0 values' to 'NULL'.

Does that answer your question ?

Sylvain


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