-->
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: Annotations and Composite primary keys question
PostPosted: Fri Apr 08, 2005 4:43 pm 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0 w/ annotations 3.0 beta1

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: Hypersonic SQL

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


I'm trying to implement a relation table with a composite primary key. This table is supposed to associate to entity tables, TableA and TableB :
Code:
@Entity
public class TableA
{
    private Long aId;
    private Set<RelationTable> relationList = new HashSet<RelationTable>();

    ....
    @OneToMany
    @JoinColumn(name  = "a_id")
    public Set<RelationTable> getRelationList()
    {
        return relationList;
    }
    ....
}

@Entity
public class TableB
{
    private Long bId;
    ....
}



This is the relation table for "connecting" Table A and TableB. It has a composite primary key.:
Code:
@Entity
public class RelationTable
{
    private RelationTablePk relationTableId;

    public RelationTable()
    {
    }

    @Id(generate = GeneratorType.NONE)
    public RelationTablePk getRelationTableId()
    {
        return relationTableId;
    }

    public void setRelationTableId(final RelationTablePk id)
    {
        this.relationTableId= id;
    }
}


This is the primary key class for the above relation table:
Code:
@Embeddable
public class RelationTablePk implements Serializable
{
    private TableA tableA;
    private TableB tableB;

    public RelationTablePk ()
    {
    }

    @JoinColumn(name  = "a_id")
    public TableA getTableA()
    {
        return tableA;
    }

    public void setTableA(final TableA a)
    {
        this.tableA= a;
    }

    @JoinColumn(name = "b_id")
    public TableB getTableB()
    {
        return tableB;
    }

    public void setTableB(final TableB b)
    {
        this.tableB = b;
    }

The above relationship mapping between the RelationTable and the RelationTablePk is working fine. The problem I have is in TableA where I want to get a list of all the RelationTables it is associated with. When I try to get the size of the list in my test class, I get the following exception:

Code:
org.hibernate.exception.GenericJDBCException: could not initialize a collection:


At this point, I want to make sure what I'm trying to do is using the right annotations. Any input would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 12, 2005 5:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't get your use case.
Why don't you use a many to many between A and B using RelationTable as the association table?

Code:
public class A {
  @ManyToMany
  @AssociationTable(table=@Table(name="RelationTable"), ...)
  Set<B> getBs() {

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 12, 2005 10:48 am 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
Yup. I ended up doing that. :) Thanks for the reply though!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 1:23 pm 
Newbie

Joined: Wed Apr 05, 2006 4:33 pm
Posts: 5
What I don't get is what to do when the RelationTable has some data of its own that's important (in my case, you have a recipe, an ingredient and the RelationTable is ingredient_quantity, with an extra double value).
How do you map *that*?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 7:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
through an intermediate entity

_________________
Emmanuel


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.