File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about the type of JoinColumn.

 
Mellon Sun
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mellon Sun
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's funny.
After dinner, I try it again. No error any more.
Oh, God.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems it was waiting for your dinner. You did that, your problem solved.
Would you share what exactly it was?
 
Mellon Sun
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mellon Sun
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 270
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mellon Sun
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not saying it's wrong of NetBeans6.5.
I prefer to say the book given a mistake sample.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic