File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Question about the type of JoinColumn. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Question about the type of JoinColumn." Watch "Question about the type of JoinColumn." New topic
Author

Question about the type of JoinColumn.

Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
In my Entity OAUser.java, I defined this:

@OneToOne
@JoinColumn(name="roleID",referencedColumnName="id")
private OARole role;

In my Entity OARole.java, there are only two field:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String roleName;

When TOPLink generate tables, the type of column roldID in table OAUser is varchar(255), and the foreign key generation failed, why?

Toplink execute "ALTER TABLE OAUSER ADD CONSTRAINT FK_OAUSER_roleID FOREIGN KEY (roleID) REFERENCES OAROLE (id)" and encounter an error, because the roleID column in OAUSER is varchar type and the id in OAROLE is number type.


SCJP,SCWCD1.3,SCWCD1.4,SCJD,SCBCD5,SCEA5
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
It's funny.
After dinner, I try it again. No error any more.
Oh, God.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Seems it was waiting for your dinner. You did that, your problem solved.
Would you share what exactly it was?


cmbhatt
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
I get the difference.
Won't work:
@OneToOne
@JoinColumn(name="roleID",referencedColumnName="id")
private OARole role;

Works well:
@OneToOne
@JoinColumn(name="roleID")//,referencedColumnName="id")
private OARole role;

I don't know why yet.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Works well:
@OneToOne
@JoinColumn(name="roleID")//,referencedColumnName="id")
private OARole role;

I don't know why yet.


Can you confirm in your database, whether roleID has a join with any column or not? If yes with which Column?


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
I am sure when annotated like:

@OneToOne
@JoinColumn(name="roleID",referencedColumnName="id")
private OARole role;

the OAUSER table will generate but the column roleID is defined as varchar(255) and no Foreign Key constraint is defined for the column roleID.

if erase the attribute referencedColumnName="id":

@OneToOne
@JoinColumn(name="roleID")//,referencedColumnName="id")
private OARole role;

the roleID column in table OAUSER is defined as number(19) and a Foreign Key constraint is defined for the column roleID as well. My program goes
on and insert one row in each table.
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
"If you are joining on something other than the primary-key column
of the ADDRESS table, then you must use the referencedColumnName( ) attribute.This referencedColumnName() must be unique, since this is a one-to-one relationship." I find this in "Enterprise Java Beans 3.0", it seemed that I misunderstand the referencedColumnName attribute. In my case, the roleID in OAUser is a foreign key reference the OARole table's id column which is defined as PK of OARole table.
Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

How come the below code given in EJB in Action is working and the above is not working?



I dont see any difference between the above code and code posted by mellon sun.

Regards
Sudhakar


Regards
Sudhakar
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
No difference edition doesn't work in my NetBeans6.5 before I comment that referencedColumnName="BILLING_ID" attribute ("roleID" in my code).
Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

No difference edition doesn't work in my NetBeans6.5 before I comment that referencedColumnName="BILLING_ID" attribute ("roleID" in my code).


are you saying that this is a problem with the Netbeans version?In that case i would say no because i am using Netbeans 6.0 and i am facing the same problem you faced(uncommenting referencedcolumnName worked for me).

Regards
Sudhakar
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
I am not saying it's wrong of NetBeans6.5.
I prefer to say the book given a mistake sample.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question about the type of JoinColumn.
 
Similar Threads
difference between joinColumn and PrimaryKeyJoinColumni
Hibernate association mapping
No metadata was found for type "class java.util.ArrayList". The class is not enhanced
Cannot add or update a child row: a foreign key constraint fails JPA/Hibernate
mapping One-to-many relationships using NetBeans and Not Hibernate