使用 EJBQL 的 LEFT JOIN 表
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果两者之间没有关系,并且您确实只需要
Machine
对象,则可以将LEFT JOIN FETCH
更改为IN< /code> 语句,如(伪代码):
If there is no relation between the two, and if you really only need the
Machine
object, what you can do is change theLEFT JOIN FETCH
to anIN
statement, as in (pseudo-code):