• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

@ManyToOne & @JoinColumn

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to EJB and JPA and I need your help please.

Suppose that we have two Entities A (mapped to DB table A with Pk field named id)
and B (mapped to a DB table B with FK a_id that references A.id in a many to one relationship)

in entity class A I have:


in entity class B I have:


Based on my understanding that should work but Eclipse is giving me referenced column "id" in join column"a_id" cannot be resolved.
I checked eclipse's manual and it says that i need to make sure that the column exists in DB and it does, so why am I getting that error?

Your help is greatly appreciated,
Musab
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your topic says it all. I think you need an @JoinedColumn tag above the Collection. When I just have @ManyToOne and I let the tables be automatically created, then what happens if you have the parent and child tables, but it also creates a join table. When I add @JoinedColumn, it just creates parent and child tables where the child table gets a foreign key reference.

Mark
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark,
you gave me hope after I almost lost it

Tables are already created with FK constraint and everything in DB is set. Actually I am using the auto creation feature.

I am just trying to setup Many-to-one association between the two entities using @JoinColumn and it is not happening (gives the cannot be resolved error)

any idea?
thanks
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried the following?

 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Musab Al-Rawi:

in entity class B I have:



I am sorry what I actually have is what Sergio suggested and that's what I am getting the error for.

So to recap this is what I have in entity class B (and it is causing an error)

[ September 24, 2008: Message edited by: Musab Al-Rawi ]
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post both classes' code in completion?
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Users table has primary key column named id
Groups table has a primary key column named id and a FK named user_id that references users.id


This is the One to many side of the relationship:


This is the Many to one side
I haven't done the many to many relationship for this class yet:

[ September 24, 2008: Message edited by: Musab Al-Rawi ]
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Musab,

Try changing the User class to the following:



I don't think the @GeneratedValue annotation works on String attributes/fields. Also, you don't need to specify the @Column annotation if the column name is identical to the class' attribute/field name.
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sergio thanks for the tips, I made the modifications and the problem is still there.

I noticed that this problem only happens with the USER table! The table is created and there is a column called id that acts as a PK.
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone any suggestions please?
reply
    Bookmark Topic Watch Topic
  • New Topic