1

I have an app to develop where it contains many tables of database, I gone through some of the tutorials online, and I want to ask if I have many tables that are not connected to each other (just for storing and displaying purposes, no manipulation etc), are each of the tables must have their own Java class?

2 Answers 2

1

If you want to have an ORM (for a concrete example please take a look here) and if these tables have nothing in common with each other, then yes, it's better to have distinct class for every table. But if you are not aimed to use ORM, then you don't need even classes, you are free to use data you get from DB on your own. Also please note, that you should have one class for serving DB requests. If you are using SQLite, it may be the class extending SQLiteOpenHelper and it's better to build it as singletone.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeap, I want it to have as an ORM, and to me the tables have nothing in common for example, a house table and a car table in which have fields such as id, description, and types. but if i do it in separate classes, how can I use search any data out of the two tables when user perform search? I mean, there will be a textfield allowing user to input say, "Porche", but i want it to do search to all of my tables (house and car tables). Is it possible if each of the tables have different class?
If you have common fields, probably the best way to go is to have a root table with the common fields and a table for every type with specific fields.
@Chrisantics I don't think it's the question of ORM, it's more question about constructing tables and writing dependencies. One house can have many cars, it's dependency between tables. So when you are done with tables, you can reflect these dependencies at your classes. Take a look on few examples here github.com/j256/ormlite-jdbc/tree/master/src/test/java/com/j256/…
0

One class for database (and in fact, for application) is enough. There is no need for maintaining each table in separate class. It is really easier to maintain all tables in one class. Additionally, if you'd like to create join of two tables, it would be really tough thing to do using multiple classes. With one class it can be done internally there.

Of course, if you would have to maintain many classes (20 for example) it would be a good idea to group them depends on what the store. But one class per table is really not necessary

4 Comments

androidhive.info/2013/09/… This is quite useful and representative.
@Chrisantics - see the example I posted above
Yes, i saw it and you have multiple model classes (or, POJO's) per table. Did we get confused over the terms "database" and "table"?
@chrispolzer - well, maybe :) What I meant is that you do not have do create separate java class (extending SQLiteOpenHelper etc.) for each of the tables stored in database - one class is enough. But for Object-Relational Mapping one class for data from one table is quite reasonable. I can easily imagine working with SQLite without converting data in database to objects (returning only hashmaps with data or sth.), but yes - if we are talking about mapping database data to objects - one class for table is appropriate. In conclusion - the example from link is the best option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.