-->
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.  [ 8 posts ] 
Author Message
 Post subject: Generate ID at creation-time
PostPosted: Tue Jan 29, 2008 5:10 am 
Newbie

Joined: Tue Jan 15, 2008 6:26 am
Posts: 8
Hi,

is there a chance to let hibernate create the ID at creation-time of an entity?

I´ve got the problem, that I want to share one id between multiple entities while having a unidirectional one-to-one relation.
This way I only have a generated value at the parent-object and for the dependent entities I have to set the id manually (to the same id, as the parent). Unfortunately the id is only generated when persisting the entity (before that the value is null), so it´s hard to propagate an object-reference to the dependant entities (even with @PrePersist as I would have to parse the parent-object).
So what I could do is to manually get the next value from my sequence and set this as an id for every entity. But if the id would be generated when a new entity is created, there won´t be a problem at all. Is there a way to achive this? I already looked at the @Generated-annotation, but this doesn´t seem to solve this issue.

Any input would be greatly appriciated.

Thomas



Hibernate version:
3.3.0

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:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 4:59 am 
Newbie

Joined: Tue Jan 15, 2008 6:26 am
Posts: 8
Now I saw something like this, which seems to go in the same direction:

Code:
public class Parent {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id

@OneToOne
@PrimaryKeyJoinColumn
private Child child;
}

public class Child {

@Id
@GeneratedValue(generator="foreignKeyGenerator")
@org.hibernate.annotations.GenericGenerator(name="foreignKeyGenerator", strategy="foreign",parameters=@Parameter(name="property", value="parent"))

@OneToOne(optional=false)
@JoinColumn(name="id")
private Parent parent;
}


The question is now: does this also work for a uni-directional relationship or do I have to have a bi-directional relationship for this to work?

Thomas


Top
 Profile  
 
 Post subject: Bi-directional is required
PostPosted: Thu Feb 14, 2008 2:37 pm 
Newbie

Joined: Thu Feb 14, 2008 1:41 pm
Posts: 14
I've read a few post and it needs to be bi-directional to work, I've tried the same and failed. Read this post, someone was successful with it,

http://opensource.atlassian.com/project ... e/HHH-2712

http://www.i-proving.ca/space/Technolog ... ne+Mapping

http://saloon.javaranch.com/cgi-bin/ubb ... 8&t=002219

Here is a link to my post, of what all I've tried, if you were able to solve the problem, please let me know how were you able to do so. Thanks

http://forum.hibernate.org/viewtopic.ph ... 23#2376623


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 4:22 am 
Newbie

Joined: Tue Jan 15, 2008 6:26 am
Posts: 8
varunmehta,

thanks for your answer. I didn´t find a solution to this as well until today. So I decided to use bi-directional relations and hope, that there might be a solution some time. But right now it works for me (with a little overhead and a little unnecessary relations)

As you already wrote in your posting, the problem seems to be with the generator annotations. Also in my scenario, when I debug into the code, somehow my parent-object resolves to null when persisting to database and therefor causes an error when trying to get the parent´s id. If I remove the @GenericGenerator-annotation and assign the id manually everything works fine, so I guess there are still some problems when using the @GenericGenerator-annotation anyway

Thomas


Top
 Profile  
 
 Post subject: GenericGenerator is the problem?
PostPosted: Fri Feb 15, 2008 11:47 am 
Newbie

Joined: Thu Feb 14, 2008 1:41 pm
Posts: 14
I manually assigned the id, removed the GenericGenerator for the class and it worked like the charm. One question though, have you tried it without annotations and using hbm files? Cos that's the next thing I plan to try...

If it's a problem then it might be a bug with the GenericGenerator.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 11:53 am 
Newbie

Joined: Tue Jan 15, 2008 6:26 am
Posts: 8
yep, that´s what I did as well.

No I´ve never used hbm-files, because I use hibernate in an EJB3-environment and therefor rely on annotations from javax.persistence (except the @GenericGenerator, because that´s out of specs).

I also think, there is probably a bug in the GenericGenerator.


Top
 Profile  
 
 Post subject: Re: Generate ID at creation-time
PostPosted: Sat Mar 02, 2013 10:43 pm 
Newbie

Joined: Sat Mar 02, 2013 10:37 pm
Posts: 2
So much time gone from last reply in this topic. All links above - i already visited. This all not work. Is it possible use Generic generator in children of unidirectional relation? So much time gone. Maybe something change?


Top
 Profile  
 
 Post subject: Re: Generate ID at creation-time
PostPosted: Mon Mar 04, 2013 6:05 pm 
Newbie

Joined: Mon Mar 04, 2013 5:41 pm
Posts: 2
Hello!
Thanks for nice shearing....
I am also new in here.

_________________
[url=www.chambre-dhote.net]chambre d'hote[/url]

[url=www.vakantiehuisjefrankrijk.be]vakantiehuis frankrijk[/url]


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