• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

DAO classes, foreign key and join query

 
Greenhorn
Posts: 7
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all, i need some suggestion for a DAO implementation of a database.
First question.
How i represent a foreign key into a dao class? for example, i have this two tables:

book(id, title, author)
author(name, age)

where book(author) is a foreign key on author(name)

Just i need to code foreign key as a String (in this case) like



Or is not a good practice?

Second question.
With dao i can do CRUD operations, but if i need to make a query that join two or more table, how can i do?
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd use JPA, with Hibernate as implementation of choice in a JSE environment. That way, you can use annotations to mark the relationship, and JPA will do the rest for you. In your case your Book class would get a property of type Author.

Note however that what you've shown is not a DAO (data access object), but sooner an entity (in JPA terms) or DTO (data transfer object).
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya I completely agree with Rob .
You can check this nice article : webpage

Thanks ,
Satya
 
giuseppe gio
Greenhorn
Posts: 7
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ops, sorry, i've made a mistake, it was a DTO representation.
I don't want to use a specific framework, i just want to understand some design principle, and then i'll use framework.
So into DTO class, foreign key it's something like




Now, when i need to do a query that need a join of two or more table, what's the best way to implement classes?

thanks for the answers.
 
Create symphonies in seed and soil. For this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic