Search code examples
javadatabasejdbcderbyjavadb

How to store Java Objects in JavaDB Derby Database?


I have a java class to store Media objects for a movie/TV/DVD index. I'm planning to try to store these objects in a JavaDB database. It will have many thousands of entries.

I have read about serializing objects, and also a bit about hibernate and the persistence API, although I don't really understand how they work. I'm just getting confused on what exactly I should be learning how to do. Should I worry about these, or instead just make a table with all the info in VARCHAR columns?

Can anyone give me any tips on what system they would use to store all this info into a database? Or maybe just some general pointers on what you would do, and why?

Obviously I'm a newbie with databases. I have just done this codejava tutorial and can successfully create/connect to a JavaDB database.

Thanks!

public class Media {

    File file_location;
    String filename;
    Date date;
    int hres;
    int vres;
    boolean has_tbn;
    File tbnloc;
    boolean has_ss;
    File ssloc;
    int length;
    String[] actors;
    String[] tags;
    boolean HD;
    long filesize;
    String md5 = "origMD5";

    Media(File thefile) {
        this.file_location = thefile;
    }

    Media(String thefile) {
        this.file_location = new File(thefile);
    }
// snipped getters and setters
}

Solution

  • ORM - Object Relational Maping is a concenpt that Hibernate and other frameworks use to store the data into database. It's an option to make easier to persist your data.

    Using Hibernate Framework gives your opportunity to think about a database row like an object itself (Media object) there the columns of this row are Media object fields. By using an Pojo class you can have access to all of these fields and set/get the fields as you wish, and using Hibernate API you can persist the object into your database.

    Depending on this concept you can choose to use Hibernate Framework or use default JDBC.

    A good tutorial about hibernate you can find here: http://www.tutorialspoint.com/hibernate/index.htm