File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Converting Java classes to relational database tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Converting Java classes to relational database tables" Watch "Converting Java classes to relational database tables" New topic
Author

Converting Java classes to relational database tables

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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 ]

SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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 ]

    Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
    Veena Pointi
    Ranch Hand

    Joined: Jun 20, 2002
    Posts: 442
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Converting Java classes to relational database tables