• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Hibernate Annotation Non Primary Key Foreign Key Mapping with VARCHAR data type

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgive my ignorance, but i've been trying to google/yahoo this thing for 2 days in a row already.
Hope you can shed me some light.

I have these database objects/tables below:



As you can see i have issuingbank table with a child table bankaccount with a onetomany relationship (wrt to issuingbank).
bankaccount.bankcode has a foreign key defined that references to issuingbank.bankcode.

I did my Hibernate Annotation mapping below:



When I try to load all issuingbank data EAGERLY using getHibernateTemplate.loadAll(IssuingBank.class),
I was able to load all IssuingBank data, and only some BankAccount data, even though the FETCHTYPE is EAGER. Further investigation (or maybe I am wrong),
is that I noticed that all data with issuingbank.bankcode value parsable to Integer
are loaded, while all data with issuingbank.bankcode value not parsable to integer were not loaded.
ANd when I try to load all BankAccount using getHibernateTemplate(BankAccount.class), I get and exception:

Caused by: java.sql.SQLException: Invalid value for getInt() -

It tried accessing all bankcode value as an int, but since it cant be cast it's an error.
Why did it tried to access the value via getInt() when in fact in the database it is varchar? Have I done something wrong in my map?

Also, how come the generated query is like this:



it tried to join via bankaccount.bankcode and issuingbank.id; i have explicitly define that the mapping should be issuingbank.bankcode to bankaccount.bankcode.

I would be very glad if you tell me what I've don wrong.

Thanks
-marckun

 
Marc Heruela
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Additional Info:
Im using Hibernate, Spring Struts (Using Hibernate Annotation), on MySQL 5.1 RDBMS.

Furthermore:

my foreign key bankaccount.bankcode references to issuingbank.bankcode which is not a primary key, but a unique key.

(i think the above is the caused of the problem since the primary key of issuingbank is an integer; explains the getInt() part,
but i dont know why)
 
Marc Heruela
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i found the answer. i dont know why but adding referenceColumn name inside @JoinColumn annotation solves the problem.



please advise if my solution is correct.

Thanks
-marckun
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/* in issuingbank */
@OneToMany(mappedBy = "issuingBank", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public Collection<BankAccount> getBankAccounts() {
return bankAccounts;
}

/* in bankaccount */

@ManyToOne
@JoinColumn(name = "bankcode", referencedColumnName = "bankcode")
public IssuingBank getIssuingBank() {
return issuingBank;
}

try the above code.
@joincolumn is not needed both sides
"mappedby" should be used at owner side of both entities (nothing but who is responsible for querying).
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic