java ORM for a read only DB

佐手、 提交于 2019-12-10 14:55:41

问题


I know hibernate but I wonder if there would be a lighter ORM engine for a read only database. I mean, I don't need some transactional queries or to update some records. On the other side, I need to handle some large list of records:

List<MyRecord> list= object.getMyRecords(); // list.size() > 1E7

Does such engine exist ? Many thanks,

Pierre


回答1:


You can check out JIRM (yes, that is a shameless plug) which is an ORM focused on using immutable objects.

It sits right on top of Spring JDBC so, if you're using Spring, it's very low risk to try it.

It also has really nice SQL placeholder templates that do not require Spring.

Another project you should checkout is jOOQ.




回答2:


I think you should consider MyBatis (IBatis 3) ORM library. It fits the requirements from light-weight work to some heavy batch executions. So it'll fit your current needs very well and won't through you out of luck when you require some heavy-lifting work from your underlying ORM library.




回答3:


If you are looking for a lightweight ORM framework, look no further than OrmLite. It allows you to avoid most of the overhead of setting up more complicated ORM frameworks like Hibernate. The downside to this framework is that it does not support standard JPA annotations so it would not be easy to swap this ORM out for a different one later on.

I would suggest trying out OpenJPA which is a JPA standard compliant ORM framework maintained by the Apache foundation. It is one of Hibernate major competitors. It does have a bit more overhead to setup but it has the advantage of allowing you to swap it out in the future for a different JPA compliant framework (like Hibernate) if it no longer suits your needs.

Both of these frameworks are Open Source as well which is always a good reason for using them.




回答4:


I searched lite ORM too for one project about a year ago. And tried a couple of them. I was a bit unsatisfied with all solutions because each of them was trying to move in certain direction.

Hibernate is a huge and it can be a source of extra troubles when you want just simple solution. Currently I am working on the one nice project it uses little custom layer over tables.

You can probably try to implement a little layer over your database with JDBC using some patterns like Active Record or Table Gateway, etc.

Any ORM solution just tries to be very unified and ads extra tails to your project. But you can create your own custom layer-less solution - this will especially work fine if you database have predefined set of tables and doesn't change it's structure too often.




回答5:


Have a look at Ebeans which is a light ORM tool:

  • Reuses JPA Annotations (@Entity, @OneToMany ...) for mapping.
  • Uses a Session Less architecture (i.e. has a simpler API than JPA).
  • Supports immutable Value Object.
  • Allows to use ORM or Relational features
  • Claims to provide good large query support

Worth the look IMO.




回答6:


Spring's SimpleJDBCTemplate combined with result mappers worked marvels for me in read only scenarios, you don't get the unneeded overhead of hibernate, all your pseudo-ORM mapping is bundled together, it is a true win for such scenarios.




回答7:


If you are looking for a lightweight ORM I would recommend jORM. Note that my answer is biased, since I'm one of the contributors to the project.

The main reasons why we decided to write a lightweight alternative are (the need for):

  • Hazzlefree configuration
  • Clear transaction definitions
  • Memory management and speed
  • Simple code generation
  • Hand tuned SQL

In short; rapid development.

Example configuration

DataSource dataSource = new DataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/moria");
dataSource.setUsername("gandalf");
dataSource.setPassword("mellon");

Database.configure("moria", dataSource); // now there's a named database

Example record

@Jorm(database="moria", table="goblins", id="id")
public class Goblin extends Record {  
    public Integer getId() {
        return get("id", Integer.class);
    }
    public void setId(Integer id) {
        set("id", id);
    }
    public Integer getTribeId() {
        return get("tribe_id", Integer.class);
    }
    public void setTribeId(Integer id) {
        set("tribe_id", id);
    }
    public Tribe getTribe() {
        return get("tribe_id", Tribe.class);
    }
    public void setTribe(Tribe tribe) {
        set("tribe_id", tribe);
    }
    public String getName() {
        return get("name", String.class);
    }
    public void setName(String name) {
        set("name", name);
    }
}

Example mapped queries

Goblin goblin = Record.findById(Goblin.class 42);

List<Goblin> goblins = Record.findAll(Goblin.class);Goblin bolg = Record.find(Goblin.class, new Column("name", "Bolg"));

Example custom queries

Tribe tribe = new Tribe();
tribe.setId(1);
String name = "Azog";

Goblin azog = Record.select(
    Goblin.class,
    "SELECT * FROM goblins WHERE name = #1# AND tribe_id = #2:id#",
    name, // #1#
    tribe // #2#
);

Goblin bolg = Record.find(Goblin.class, "SELECT * FROM goblins WHERE name = #1#", "Bolg");


来源:https://stackoverflow.com/questions/3491540/java-orm-for-a-read-only-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!