这是一个开放性的问题。我将开始一个新的项目,正在寻找不同的orm与数据库访问集成。

你有最喜欢的吗? 有什么你建议不要碰的吗?


当前回答

虽然我也担心Java会取代自由形式的SQL查询,但我确实认为人们批评ORM是因为它的应用程序设计总体上很糟糕。

True OOD is driven by classes and relationships, and ORM gives you consistent mapping of different relationship types and objects. If you use an ORM tool and end up coding query expressions in whatever query language the ORM framework supports (including, but not limited to Java expression trees, query methods, OQL etc.), you are definitely doing something wrong, i.e. your class model most likely doesn't support your requirements in the way it should. A clean application design doesn't really need queries on the application level. I've been refactoring many projects people started out using an ORM framework in the same way as they were used to embed SQL string constants in their code, and in the end everyone was suprised about how simple and maintainable the whole application gets once you match up your class model with the usage model. Granted, for things like search functionality etc. you need a query language, but even then queries are so much constrained that creating an even complex VIEW and mapping that to a read-only persistent class is much nicer to maintain and look at than building expressions in some query language in the code of your application. The VIEW approach also leverages database capabilities and, via materialization, can be much better performance-wise than any hand-written SQL in your Java source. So, I don't see any reason for a non-trivial application NOT to use ORM.

其他回答

Hibernate,因为它基本上是Java中事实上的标准,并且是创建JPA的驱动力之一。Spring对它有很好的支持,几乎每个Java框架都支持它。最后,GORM是一个非常酷的包装器,它使用Groovy做动态查找器等。

它甚至被移植到。net (NHibernate),所以你也可以在那里使用它。

当我在编写一个中等大小的JavaSE应用程序时,我对Avaje Ebean有过很好的体验。

它使用标准JPA注释来定义实体,但公开了一个更简单的API(没有EntityManager或任何附加/分离实体之类的废话)。它还允许您在必要时轻松使用SQL查询或事件纯JDBC调用。

它还为查询提供了一个非常好的流动和类型安全的API。你可以这样写:

List<Person> boys = Ebean.find(Person.class)
                                  .where()
                                       .eq("gender", "M")
                                       .le("age", 18)
                                  .orderBy("firstName")
                                  .findList();

虽然我也担心Java会取代自由形式的SQL查询,但我确实认为人们批评ORM是因为它的应用程序设计总体上很糟糕。

True OOD is driven by classes and relationships, and ORM gives you consistent mapping of different relationship types and objects. If you use an ORM tool and end up coding query expressions in whatever query language the ORM framework supports (including, but not limited to Java expression trees, query methods, OQL etc.), you are definitely doing something wrong, i.e. your class model most likely doesn't support your requirements in the way it should. A clean application design doesn't really need queries on the application level. I've been refactoring many projects people started out using an ORM framework in the same way as they were used to embed SQL string constants in their code, and in the end everyone was suprised about how simple and maintainable the whole application gets once you match up your class model with the usage model. Granted, for things like search functionality etc. you need a query language, but even then queries are so much constrained that creating an even complex VIEW and mapping that to a read-only persistent class is much nicer to maintain and look at than building expressions in some query language in the code of your application. The VIEW approach also leverages database capabilities and, via materialization, can be much better performance-wise than any hand-written SQL in your Java source. So, I don't see any reason for a non-trivial application NOT to use ORM.

SimpleORM,因为它是直接的,没有魔法。它在Java代码中定义了所有元数据结构,非常灵活。

SimpleORM provides similar functionality to Hibernate by mapping data in a relational database to Java objects in memory. Queries can be specified in terms of Java objects, object identity is aligned with database keys, relationships between objects are maintained and modified objects are automatically flushed to the database with optimistic locks. But unlike Hibernate, SimpleORM uses a very simple object structure and architecture that avoids the need for complex parsing, byte code processing etc. SimpleORM is small and transparent, packaged in two jars of just 79K and 52K in size, with only one small and optional dependency (Slf4j). (Hibernate is over 2400K plus about 2000K of dependent Jars.) This makes SimpleORM easy to understand and so greatly reduces technical risk.

冬眠,因为它:

是稳定的-存在了这么多年,它没有任何大的问题 规定了ORM领域的标准 实现标准(JPA),并对其进行口述。 网上有很多关于它的信息。有许多教程,常见问题的解决方案等 功能强大—您可以将非常复杂的对象模型转换为关系模型。 它支持任何主要和中型RDBMS 一旦你学会了它,就很容易使用

关于为什么(以及何时)使用ORM的几点:

you work with objects in your system (if your system has been designed well). Even if using JDBC, you will end up making some translation layer, so that you transfer your data to your objects. But my bets are that hibernate is better at translation than any custom-made solution. it doesn't deprive you of control. You can control things in very small details, and if the API doesn't have some remote feature - execute a native query and you have it. any medium-sized or bigger system can't afford having one ton of queries (be it at one place or scattered across), if it aims to be maintainable if performance isn't critical. Hibernate adds performance overhead, which in some cases can't be ignored.