Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting Java classes to relational database tables

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • 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 ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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: 442
    • Mark post as helpful
    • send pies
    • 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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic