Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What should I search/research if I'm looking for how to get started with translating database tables to Java code? For instance, if I have a one-many relationship in the database and I want to translate that to java code...what is that process called?

I'm really looking for just plain Java code example and not any tools at the moment.

share|improve this question
1  
Your question is somewhat unclear. What do you mean by "translating"? Do you mean how to design objects that encapsulate and represent the data queried from the database? –  Brian Roach Dec 5 '11 at 1:02
    
Yes for instance. If I have one survey with many comments. How would I model that in Java or if I have many recipes that have many tags (many-to-many). –  Robert Dec 5 '11 at 1:07
    
@Robert, you do not want to map everything. You want to have "getters" and "setters" stored procedures and maybe map their input and output instead of mapping every table to some object. Modern DBMS are amazing at doing things fast and correct. Why not leverage it? In your case, have a proc that returns everything you need from the comment table given the survey id. Then manually store those comments in a collection. I have done somethign like that in C#. I would have a CommentEntry class with members mirroring columns wo code generation. foreach(row in res) { lst.add(new CommentEntry(row))} –  Job Dec 5 '11 at 3:41

3 Answers 3

up vote 3 down vote accepted

If I understand your question correctly I think you are looking for the basic object/relational mapping patterns. They will tell you about tested and proven ways of mapping different types relationships between OO code and relation models, as well just basically handling the mismatch between an object in the application code and a row in the DB. The best place to start learning about these patterns is Martin Fowler's book Pattern of Enterprise Application Architecture, or if don't want a book his web site: http://martinfowler.com/eaaCatalog/

share|improve this answer
    
This is exactly what I was looking for. Thank you. –  Robert Dec 5 '11 at 9:30

First, learn about SQL (tables, queries, joins, transactions), then JDBC (statements, cursors, bind variables). Basic SQL is rather simple, as is basic JDBC. This will let you at least implement the simple display / create / update / delete sort of operations.

Distant next, learn about ORMs (such as Hibernate).

By that time you'll be able to ask more specific questions here.

share|improve this answer

It is a form of code generation. You can get the information from the database using SQL queries. For instance, to get the column names from table "person" you would do this:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'person'

That would give you the column names which you can then generate getters and setters based on each column name and type (which you can also get from the INFORMATION_SCHEMA).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.