-->
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.  [ 3 posts ] 
Author Message
 Post subject: Two Many-to-one relationship to same table
PostPosted: Wed May 25, 2005 10:43 am 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.4 and Annotations 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:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I have the following two classes:
Code:
@Entity
public class ClassA
{
    private Long id;
    private ClassB firstParty;
    private ClassB secondParty;

    @Id(generate = GeneratorType.AUTO)
    public Long getId()
    {
        return id;
    }

    @ManyToOne
    @JoinColumn(name = "first_party_id", referencedColumnName = ClassB.PK_COLUMN_NAME, nullable = false)
    public ClassB getFirstParty()
    {
         return firstParty;
    }

    @ManyToOne
    @JoinColumn(name = "second_party_id", referencedColumnName = ClassB.PK_COLUMN_NAME, nullable = false)
    public ClassB getSecondParty()
    {
        return secondParty;
    }

    // Corresponding setters are here
    // ......
}

Here's the second class that the first class has a relationship to:
Code:
@Entity
public class ClassB
{
    public static final String PK_COLUMN_NAME = "id";

    private Long id;
    private Set<ClassA> firstRelationships = new HashSet<ClassA>();
    private Set<ClassA> secondRelationships = new HashSet<ClassA>();

    @Id(generate = GeneratorType.AUTO)
    @Column(name = PK_COLUMN_NAME)
    public Long getId()
    {
        return id;
    }

    @OneToMany(mappedBy = "firstParty")
    public Set<ClassA> getFirstRelationships()
    {
        return firstRelationships;
    }

    @OneToMany(mappedBy = "secondParty")
    public Set<ClassB> getSecondRelationships()
    {
        return secondRelationships;
    }

    // Insert corresponding setters here
    // .......
}


My question is that is this mapping the right way to go? I don't have any errors for this but whenever I run test cases for these kind of classes, it takes a lot longer to execute. I wanted to make sure the design itself wasn't flawed.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 10:46 am 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
Oops. I made a typing error in the second class:
Code:
public Set<ClassB> getSecondRelationships()

should be
Code:
public Set<ClassA> getSecondRelationships()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 11:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Should work fine.
Remove referencedColumnName though, this is not necessary

_________________
Emmanuel


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