使用 EJBQL 的 LEFT JOIN 表

发布于 2024-11-06 09:31:28 字数 940 浏览 1 评论 0原文

我正在尝试使用 EJBQL(带有底层 MySQL 数据源)连接两个 EJB。有问题的两个表是

Machine
  - Hostname
  - ... unrelated fields ...

Location
 - Code
 - Human readable description

这些表应该在位置代码和计算机主机名的前三个字符上左连接。在直接 MySQL 中,命令是:

SELECT * FROM machine m LEFT JOIN location l ON (SUBSTRING(m.`Name`, 1,3) = l.`Code`);

当我将类似的东西放入 EJBQL 中时,我收到各种错误,从 null poninter 异常到无效语法异常 - 这是我尝试过的:

query="SELECT NEW someObj(m, loc) FROM Machine as m " +
      "LEFT JOIN FETCH Location as loc " +
      "WHERE (SUBSTRING(m.hostname, 1, :length) = loc.code)"

我也尝试使用“ON”而不是“WHERE”——但是当我使用 ON 时,EJBQL 会返回一个意外的关键字...

那么——以前有人成功地做过类似的事情吗? EJBQL 的文档似乎表明 LEFT join 是可以的,所以我不确定这是什么交易...

谢谢


编辑:我得到的例外是:

outer or full join 后面必须跟有路径表达式< /code> - 它被埋在与空指针交易相同的行中

编辑2:

机器和位置之间没有关系,不幸的是我无法更改它

I am trying to join two EJB's using EJBQL (with an underlying MySQL data source). The two tables in question are

Machine
  - Hostname
  - ... unrelated fields ...

and

Location
 - Code
 - Human readable description

The tables should be LEFT joined on the location code and the first three characters of the machine's hostname. In straight up MySQL the command is:

SELECT * FROM machine m LEFT JOIN location l ON (SUBSTRING(m.`Name`, 1,3) = l.`Code`);

When I put a similar thing into EJBQL I get all sorts of errors raning from null poninter exceptions to invalid syntax exceptions -- here's what I have tried:

query="SELECT NEW someObj(m, loc) FROM Machine as m " +
      "LEFT JOIN FETCH Location as loc " +
      "WHERE (SUBSTRING(m.hostname, 1, :length) = loc.code)"

I've also tried using "ON" instead of "WHERE" -- but EJBQL comes back with an unexpected keyword when I use ON...

So -- has anyone succeeded in doing something like this before? The documentation for EJBQL seems to indicate that LEFT join is OK, so I'm not sure what the deal is...

Thanks


Edit: The exception I am getting is:

outer or full join must be followed by path expression -- it was buried in the same line as the null pointer deal

Edit 2:

There is no relation between a Machine and a Location, unfortunately I cannot change that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味超标? 2024-11-13 09:31:28

如果两者之间没有关系,并且您确实只需要 Machine 对象,则可以将 LEFT JOIN FETCH 更改为 IN< /code> 语句,如(伪代码):

select from Machine m 
where (SUBSTRING(m.hostname, 1, :length) in (some kind of logic about Location)

If there is no relation between the two, and if you really only need the Machineobject, what you can do is change the LEFT JOIN FETCH to an IN statement, as in (pseudo-code):

select from Machine m 
where (SUBSTRING(m.hostname, 1, :length) in (some kind of logic about Location)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文