开发者

LEFT JOIN tables with EJBQL

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 a开发者_C百科nd 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


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜