tags:

views:

136

answers:

2

Hi

I am new to hibernate and would require a clarification for the below problem.

My main application has some existing tables which i want to use for an optional feature. Now i need to retrieve data from one existing table and process that data. Do i need to write a seperate POJO for this table ? I want only 2 columns of these table.

Also does all the tables of the DB need to be mapped with POJOs?

Please clarify

regards

+3  A: 

You only need to write POJOs for the tables you are going to use. If a table is connected to another one through a foreign key, then you need to include both. This is not big deal - you can use Hibernate Tools to do this job for you. For an existing database, they can create both the Java and xml configuration files needed. This is ideal, if you are just starting with Hibernate. Auto-generate the files and then consult them to understand how you can write your own.

In a single table you can omit some columns, if you have no need for them. However, you need to include primary and foreign keys. In general, I would recommend to always map all columns.

kgiannakakis
He doesn't need POJO mappings for m:n mapping tables, although he might be better of with, than without them
Jens Schauder
A: 

Short answer -- yes.

There are a few creative ways to try and get around creating an object, but from a good design and Hibernate best practices perspective, you do in fact want to go ahead and write the little class. It sounds like it's a very small class (empty constructor, two fields, two setters/getters). Going ahead and writing the class out will make the code that deals with that information cleaner. It is absolutely in your own best interest to just go ahead and write this class out.

Even if you weren't going to use Hibernate at all, it's still a good idea to write out a class. A row of data, be it in a comma-separated list or a database, is a basic unit of data, and it will improve your code's readability to treat it as such.

CaptainAwesomePants