• 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

Converting Java classes to relational database tables

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know weather this is right place to post this question or not.I need to convert all java objects
into database tables.For example
class Customer{
Item p;
}
class Item{
int quantity;
Brand br;
}

I have interface that has methods to retrives the above info,wrote class that implements this interface.
I can define customer as different table and Item as different table.I am confused with how to represent the thing that customer has item in database tables???
This is very low level picture I am giving.Basically I need to convert the classes like above into tables,and write queries to do the functions that are done by methods defined in interface.
How to do it?
I hope my question is clear.
Thanks
[ May 07, 2004: Message edited by: Veena Point ]
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Basically I need to convert the classes like above into tables,and write queries to do the functions that are done by methods defined in interface.
How to do it?


Assuming you, for some reason, can't use an existing ORM tool like Hibernate, here's some things you might want to consider:
  • Each class gets its own database table with a name derived from the fully qualified name, for example (e.g. "com.javaranch.MyClass" becomes "com_javaranch_myclass" and so forth)
  • Each table being created gets a single surrogate column for holding a primary key
  • For each class, go through each member field and 1) if it's of a simple type (int, long, Integer, String, etc.), create a column with the appropriate data type (INT, VARCHAR, etc.), or 2) if it's not a primitive type but one of your custom classes, create a foreign key column which points to the member field type's table


  • I have never attempted to write such mapping code, but I'm pretty sure that's a good starting point.
    [ May 07, 2004: Message edited by: Lasse Koskela ]
     
    Veena Pointi
    Ranch Hand
    Posts: 469
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much for nice reply.This is really helpfull.Is there any link or online tutorials on this concept?
    Thank you very much again
    Veena
    reply
      Bookmark Topic Watch Topic
    • New Topic