-->
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: map a bidirect many-to-one association with a joinTable ?
PostPosted: Wed Oct 25, 2006 10:46 am 
Beginner
Beginner

Joined: Fri Oct 06, 2006 2:49 am
Posts: 25
Hi all,

I am having troubles to map a bidirectional many-to-one association with a Join table?

Code:
public class A {
....
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch=FetchType.LAZY, targetEntity=B.class)
    @JoinTable(
        name="A_B",
        joinColumns=@JoinColumn(name="A_ID"),
        inverseJoinColumns=@JoinColumn(name="B_ID")
    )
    @IndexColumn(name = "position", base=0)
    private B b;
...
}

public class B {
...
@OneToMany(mappedBy="b", fetch = FetchType.LAZY, targetEntity=A.class)

    private List<A> myList = new ArrayList<A>();
...
}


I have a SQLGrammarException: Could not load an entity A

If I take a look at the sql generated, I can see that the JOIN is not done and that the inverseColumn column is used in the select clause:

Code:
select
        ...
        b0_.[color=red]B_I[/color]D as B2_24_0_
    from
        B b0_
....


This means that the joinTable annotation is ignored and Hibernate thinks that an entity A is linked to an entiy B via the foreign key B_ID.....


I do not understand where is the mistake in my mapping.......

Thanks for your help,
tiggy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 12:05 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I think I made a comment in the doc about bidir indexed collections, and it is for usre in the Hibernate Core reference documentation, have a look

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 5:50 pm 
Beginner
Beginner

Joined: Fri Oct 06, 2006 2:49 am
Posts: 25
Thx a lot for your help.

I think you pointed out the problem !

I am gonna try this tomorrow and see if it works... I hope it ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 6:31 am 
Beginner
Beginner

Joined: Fri Oct 06, 2006 2:49 am
Posts: 25
I have first tried to change the type of the collection from a List to a set and apparently my "first" problem does not come from the fact that the collection is indexed.

I thought the Join wa not generated but I made a mistake, the sql generated was for another property which linked also those table but without a joinTable !

I have isolated better the code and I can see that the JOIN is done but there is a foreign_key in my select clause which is not correct:

Code:
select
        a_b_.A_ID,
        a_b_.B_ID,
        a_.id,
        a_.validFrom,
        a_.validTo,
        a_.archivedAt,
        a_.optlock,
        a_.A_ID   <= here is my problem
    from
        A_B a_b_
    left outer join
        A a_ on a_b_.B_ID=a_.id
    where
        a_b_.A_ID=?


If you take a look at the sql generated, you can see that Hibernate assumes that I have a foreign key named A_ID at the many side. I do not understand why a foreign key in the table A is assumed...

Any ideas where is the problem ?

any help is welcome,

tiggy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 5:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I do have a working test case covering that so either you do something wrong or you use an old HA version

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