简体   繁体   中英

Java ResultSets to EJB

I work alot with databases in my java code, eg reading from, writing to the database. But I think I'm doing it wrong. I'm saying this because all my classes that does this, is static. Here's an example of a todo list class that reads and writes to a database:

public class TodoListItem () {

    public static String getItem () {

        String item;

        // For the sake of simplicity just imagine that here I call respective database
        // functions, execute preparedStatements and process the returned ResultSets

        // return item

    }

    public static void setItem (String "description") {

        // Again, imagine that I do preparedStatements and update the database

    }

}

So as you can see the methods are all static and it just doesn't seem very "Object Oriented", it just feels wrong. Somehow I think you should have a bean representing a 'TodoListItem' ie:

public class Item {

    protected String description;

    public String getDescription () { return this.description; }

    public void setDescription (String description) { this.description = description; }

}

This just feels neater more "Object oriented".. But where would I then do the database calls? I find it very hard to word this question, but hopefully someone has picked up on what I'm saying...

您基本上想使用的是http://hibernate.org/orm ,它提供了数据库表的对象表示形式,供您在应用程序中使用。

Well, in fact you are doing a lot of work by yourself with that kind of object and multiple static methods, and your are rigth, and object full of static method is at least uncomfortable. Maybe you are trying to create a Data Access Object (DAO Pattern) the hard way.

There is a mismatch between the relational databases and the object oriented world, JPA (i:e hibernate, eclipselink) try to minimize that barrier. So, when you learn about JPA spec, you will see all your database from an object oriented view, that simply speed up the domain learning and the development (there are some trade-off but that is the idea).

Now, getting into detail:

  • Is a static class a bad practice for data manipulation? R/ Most of the time YES, is highly probable that you encounter yourself reinventing concepts over and over again. You will need to manipulated transactions, concurrency, locks and so on. BUT!, there are exceptions, maybe you have a very tinny legacy system and using and ORM could be overkill, so, keep it simple, created a specialized JDBC control - DAO - and used it!.
  • Is using hibernate more a luxury compared to "if you don't use hibernate you're not a very good developer" ? R/ For lots of applications or industries the ORM concepts are mandatory (if not universal), these are the kind of concepts that you just cant have the luxury to avoid " ORM, love it or hate it " but you need to learn how to deal with it.
  • So, what now? , Sit down and teach yourself an ORM and stop writing wrappers/delegates for the entity manager (jpa concept).
  • But I really hate the ORMs! R/ There will be always alternatives, not so widely standardized but still with good support and communities, such as: jooq, mybatis, spring data, etc....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM