-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to annotate 1 to 0..1?
PostPosted: Tue Oct 02, 2007 7:40 am 
Newbie

Joined: Mon Oct 01, 2007 12:42 pm
Posts: 13
Location: Helsinki, Finland
Consider the following entities:

Code:
@Entity
public class Parent {

   @Id
   private int id;
   
   @OneToOne
   private Child child;

   // ...

}


@Entity
public class Child {
   
   @Id
   private int id;

   // ...
}



I am using Hibernate to generate the database. What I want is the following:
- Parent and child are sharing the primary key
- Foreign key Child.id --> Parent.id will be created


I have tried the following additional annotations together with @OneToOne but no success:

1) No extra annotation: column Parent.child_id and foreign key Parent.child_id --> Child.id are created
2) @PrimaryKeyJoinColumn: no extra columns but no foreign keys either
3) @JoinColumn(name="id"): Same as 2)

Option 3) would do the job if the association was @OneToMany.

Any help would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 11:36 am 
Beginner
Beginner

Joined: Tue Jun 27, 2006 6:14 am
Posts: 24
Hi,

Not an answer to your question, more like fishing for an answer to mine...

I think we are trying to do something similar, although we pre-create the tables/relationships ourselves and just use hibernate for the data access.

We have a problem with how to navigate between the Parent/Child classes and specify the Id. See this entry: http://forum.hibernate.org/viewtopic.php?t=980020

So, do you need to get the Parent object, given a Child object, if so, how do you do it?

Thanks in advance,
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 2:50 am 
Newbie

Joined: Mon Oct 01, 2007 12:42 pm
Posts: 13
Location: Helsinki, Finland
Hi there,

I want the association to be unidirectional so that Parent has a reference to Child but not the other way around. So I do not need to get the Parent object, given a Child object.

If you need to navigate both ways, you need to have an bidirectional association:

Code:
@Entity
public class Parent {

   @Id
   private int id;
   
   @OneToOne
   @JoinColumn(name="id")
   private Child child;

   // ...

}


@Entity
public class Child {
   
   @Id
   private int id;

   @OneToOne(mappedBy="child")
   private Parent parent;

   // ...
}


Unfortunately this doesn't help in my case since no foreign key will be created at all.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 06, 2007 3:06 am 
Newbie

Joined: Tue Aug 07, 2007 2:31 am
Posts: 11
I think this does what you want.

Parent:
Code:
@Entity
public class Parent {

      @Id @GeneratedValue(strategy=GenerationType.AUTO)
      private int id;
      
      @OneToOne(mappedBy = "parent", fetch=FetchType.LAZY, cascade=CascadeType.ALL, optional=true)
      private Child child;
}


Child with the same ID as the parent:
Code:
@Entity
public class Child {
   @Id @GeneratedValue(generator="parentPKGenerator")
   @org.hibernate.annotations.GenericGenerator(
         name = "parentPKGenerator",
         strategy ="foreign",
         parameters = @org.hibernate.annotations.Parameter(name = "property", value = "parent")
   )
   private int id;

   @OneToOne(optional=false, fetch=FetchType.LAZY)
   @PrimaryKeyJoinColumn
   private Parent parent;
}


Generates the following for mysql:
Code:
    create table Child (
        id integer not null,
        primary key (id)
    ) type=InnoDB;

    create table Parent (
        id integer not null auto_increment,
        primary key (id)
    ) type=InnoDB;

    alter table Child
        add index FK3E104FC5F807672 (id),
        add constraint FK3E104FC5F807672
        foreign key (id)
        references Parent (id);


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 06, 2007 10:23 am 
Newbie

Joined: Mon Oct 01, 2007 12:42 pm
Posts: 13
Location: Helsinki, Finland
Thanks, that does almost what I want.

I would still like the class-level association between Parent and Child to be unidirectional, i.e. get rid of the parent field in Child class. But I guess that it is needed to get the foreign key created.


Top
 Profile  
 
 Post subject: FK constraint name
PostPosted: Mon Oct 08, 2007 7:23 am 
Newbie

Joined: Mon Oct 01, 2007 12:42 pm
Posts: 13
Location: Helsinki, Finland
It seems like the FK constraint name cannot be changed in this solution. I've tried @org.hibernate.annotations.ForeignKey in both classes but it doesn't work.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 19, 2007 4:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Post a JIRA issue for the FK name on a @OneToOne with your attached test case. This feature should be added

_________________
Emmanuel


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