LINQ for Java 工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/346721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:41:07  来源:igfitidea点击:

LINQ for Java tool

javasqlmysqllinq

提问by Milhous

Would a LINQ for java be a useful tool? I have been working on a tool that will allow a Java object to map to a row in a database.

java 的 LINQ 会是一个有用的工具吗?我一直在研究一种工具,它允许 Java 对象映射到数据库中的一行。

  1. Would this be useful for Java programmers?
  2. What features would be useful?
  1. 这对 Java 程序员有用吗?
  2. 哪些功能会有用?

采纳答案by Jon Skeet

LINQ for Java would be lovely, but the problem is the language integration.

LINQ for Java 会很可爱,但问题是语言集成。

Java doesn't have anything as concise as lambda expressions, and they're one of the bedrocks of LINQ. I suppose they couldlayer the query expression support on top of normal Java without lambda expressions, by making the expansion create anonymous inner classes - but it would be pretty hideous. You'd also need expression trees if you wanted to do anything like LINQ to SQL.

Java 没有 lambda 表达式那么简洁,它们是 LINQ 的基石之一。我想他们可以在没有 lambda 表达式的普通 Java 之上分层查询表达式支持,通过使扩展创建匿名内部类 - 但它会非常可怕。如果您想执行类似于 LINQ to SQL 的任何操作,您还需要表达式树。

Checked exceptions mightget in the way, but we'd have to see. The equivalent of IQueryable would need to have some sort of general checked exception - or possibly it could be generic in both the element type and the exception type...

检查的异常可能会妨碍,但我们必须看到。IQueryable 的等价物需要有某种通用的检查异常——或者它可能在元素类型和异常类型中都是通用的......

Anyway, this is all pie-in-the-sky - given the troubles the Java community is having with closures, I think it would be folly to expect anything like LINQ in Java itself earlier than about 2012. Of course, that's not to say it wouldn't be possible in a "Java-like" language. Groovy has certain useful aspects already, for instance.

无论如何,这都是天上掉馅饼——考虑到 Java 社区在闭包方面遇到的麻烦,我认为在 2012 年之前期待 Java 本身中的 LINQ 之类的东西是愚蠢的。当然,这并不是说在“类 Java”语言中这是不可能的。例如,Groovy 已经具有某些有用的方面。

For the library side, Hibernate already provides a "non-integrated" version of a lot of the features of LINQ to SQL. For LINQ to Objects, you should look at the Google Java Collections API- it's a lot of the same kind of thing (filtering, projecting etc). Without lambdas it's a lot fiddlier to use, of course - but it's still really, really handy. (I use the Google Collections code all the time at work, and I'd hate to go back to the "vanilla" Java collections.)

对于库端,Hibernate 已经提供了 LINQ to SQL 的很多特性的“非集成”版本。对于 LINQ to Objects,您应该查看Google Java Collections API- 它有很多相同的东西(过滤、投影等)。当然,如果没有 lambdas,它的使用会更加繁琐 - 但它仍然非常非常方便。(我在工作中一直使用 Google Collections 代码,我不想回到“vanilla”Java 集合。)

回答by yrcjaya

Hibernateuses HQL. You can do Object but only for Relational Databases

Hibernate使用 HQL。您可以执行对象,但仅适用于关系数据库

回答by Stephen

It's worth noting that Scala 2.8 is going to have LINQ support...

值得注意的是,Scala 2.8 将支持 LINQ...



Actually, scala standart collections provide API that works like LINQ-for-Objects in some sense. Here is the example:

实际上,scala 标准集合提供的 API 在某种意义上类似于 LINQ-for-Objects。这是示例:

List("Paris","Berlin","London","Tokyo")
  .filter(c => c.endsWith("n"))
  .map(c => c.length) 
// result would be length of the words that ends 
// with "n" letter ("Berlin" and "London").

Don't be scared of new-line-dot syntax: you can write code in plain old style:

不要害怕 new-line-dot 语法:你可以用简单的旧风格编写代码:

Array(1,2,3,4,5,6).map(x => x*x)

And there is a number of projects that provide close to LINQ-to-SQL syntax. For example, snippet taken from Squeryll:

并且有许多项目提供接近 LINQ-to-SQL 的语法。例如,摘自 Squeryll 的片段:

import Library._
using(session) { 
  books.insert(new Author(1, "Michel","Folco"))            
  val a = from(authors)(a=> where(a.lastName === "Folco") select(a)) 
}
// but note that there is more code behind this example

回答by Timo Westk?mper

For a more general approach to the issue, consider using Querydsl.

对于该问题的更一般方法,请考虑使用Querydsl

It provides a LINQ-style syntax with support for JPA/Hibernate, JDO, SQL and Java Collection backends.

它提供了 LINQ 风格的语法,支持 JPA/Hibernate、JDO、SQL 和 Java Collection 后端。

I am the maintainer of Querydsl, so this answer is biased.

我是 Querydsl 的维护者,所以这个答案是有偏见的。

回答by Konstantin Triger

Java LINQ to SQLimplementation. Provides full language integration and larger feature set compared to .NET LINQ.

Java LINQ to SQL实现。与 .NET LINQ 相比,提供完整的语言集成和更大的功能集。

回答by Emil Wcis?o

An extension to Java which gives LINQ-to-objects capabilities is SBQL4J. It offers:

提供 LINQ 到对象功能的 Java 扩展是SBQL4J。它提供:

  • Type-safety in compile time
  • Powerful query engine with greater capabilities than LINQ
  • Compatibility with current JVMs (it uses preprocessing to translate queries to pure Java code)
  • Nice performance (no reflection is used at runtime)
  • Clear, precise semantics without needless, obscure syntactic sugar.
  • 编译时的类型安全
  • 强大的查询引擎,具有比 LINQ 更强大的功能
  • 与当前 JVM 的兼容性(它使用预处理将查询转换为纯 Java 代码)
  • 性能不错(运行时不使用反射)
  • 清晰、精确的语义,没有不必要的、晦涩的语法糖。

回答by ajlopez

Baby steps:

宝贝步骤:

A first approach: implement Java LINQ using strings for expressions, instead of lambda.

第一种方法:使用表达式的字符串而不是 lambda 来实现 Java LINQ。

Write IQueryProvidersbased on string expressions.

IQueryProviders基于字符串表达式编写。

Then, pursue for add the string expressions directly in the language.

然后,直接在语言中寻求添加字符串表达式。

But first, I want a LINQ that works: typed linq in language, it's a nice thing, but the MAIN point is: have a way to write IQueryProviders, then, write a provider for POJOs, a provider for Hibernate, a provider for SQL Server or Oracle, etc...

但首先,我想要一个有效的 LINQ:在语言中输入 linq,这是一件好事,但主要观点是:有一种编写方法IQueryProviders,然后,为POJO编写一个提供程序,为 Hibernate编写一个提供程序,为 SQL编写一个提供程序服务器或Oracle等...

回答by F?rat Kü?üK

Sienaproject seems good.

锡耶纳项目看起来不错。