-->
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: creating composite pk with foreign key
PostPosted: Sun Feb 27, 2005 1:20 am 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Hibernate version:3.0

Mapping documents:Annotation

class myclass
{
@Id(generate=GeneratorType.NONE)
private String myid;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "myid_id")
private Collection<yourclass> yours;
}

class yourclass
{
@Id(generate=GeneratorType.AUTO)
private int id;

// how to make this part of the primary key?
@ManyToOne
@JoinColumn(name = "myid_id")
private myclass mine;
}


How can I make "mine" part of yourclass's primary key?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 1:24 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It's not officially supported yet, I thought it would work.
please provide a runnable test case and post it to JIRA in HibernateExt/Annotations
I'll have a look

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 1:45 am 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Hmmm... did not know the below code should have created the yourclass primary key (id, myid_id). My question is how to create such a key? :)
Is the below code suppose to create the above mentioned key?

Thanks :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 12:58 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
forget what I've said.
You want to use a composite key wo a PK class. this not currently possible.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 2:04 pm 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
I have looked over the example for creating a PK class. Seems easy enough, but how do I add a foriegn key to the PK class? So at this piont I understand that I need PK class, what I do not understand is how to add the foreign key to th PK class. Example...

CREATE TABLE A (
VARCHAR name NOT NULL,
primary key(name)
)


CREATE TABLE B (
INT item NOT NULL,
foreign key (name),
primary key(name,item) // this is what I want to do in hibernate
)

Thanks :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 3:10 pm 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
I looked at the dependent object example. I updated to try to make the foriegn parent pk part of the childs primary key. So, I still need help with this please....

SQL executed by Hibernate
12:41:43,953 DEBUG SchemaExport:154 - create table Parent (
firstName varchar2(255) not null,
lastName varchar2(255) not null,
age number(10,0),
primary key (firstName, lastName)
)
12:41:45,046 DEBUG SchemaExport:154 - create table Child (
firstName varchar2(255) not null,
lastName varchar2(255) not null,
parenFirstName varchar2(255) not null,
parentLastName varchar2(255) not null,
primary key (firstName, lastName)
)
12:41:45,109 DEBUG SchemaExport:154 - alter table Child add constraint FK3E104FC157CBEB0 foreign key (parenFirstName, parentLastName) references Parent
12:41:45,140 INFO SchemaExport:166 - schema export complete


Child pojo
Code:
@Entity(access = AccessType.FIELD)
public class Child implements Serializable {
    @Id(generate = GeneratorType.NONE)
    public ChildPk id;   
}



Child PK
Code:
@DependentObject(access = AccessType.FIELD)
public class ChildPk implements Serializable {
    String firstName;
    String lastName;

    @ManyToOne()
    @JoinColumns ({
            @JoinColumn(name="parentLastName", referencedColumnName = "lastName"),
            @JoinColumn(name="parenFirstName", referencedColumnName = "firstName")
    }) 
    public Parent parent;
}


Parent PK
Code:
@Entity(access = AccessType.FIELD)
public class Parent implements Serializable {
    @Id
    public ParentPk id;
    public int age;

    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumns ({
        @JoinColumn(name="parentLastName", referencedColumnName = "lastName"),
        @JoinColumn(name="parenFirstName", referencedColumnName = "firstName")
    })
    public Set<Child> children;
}
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 3:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't really get your sample, because I would have declare the many to one on the child class and not the child pk (as per your DDL). Anyway, here is the way you would declare a many to one inside a PK
Code:
@DependentObject(access=AccessType.FIELD)
public class ChildPk implements Serializable {
   public int nthChild;

   @ManyToOne()
   @JoinColumns ({
           @JoinColumn(name="parentLastName", referencedColumnName = "lastName"),
           @JoinColumn(name="parentFirstName", referencedColumnName = "firstName")
    })
   public Parent parent;

   public boolean equals(Object o) {
      //do it
   }

   public int hashCode() {
      //do it
   }
}


But it is not currently working on the annotation implementation (HBX-126) and 'll get hard time to make it work.

_________________
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.