views:

897

answers:

2

Hi all,

I have a hibernate mapping like this in a ProductDfn class

@ManyToOne( fetch = FetchType.LAZY, optional = true )
@JoinColumn( name = "productTypeFk", nullable = true )
public ProductType getProductType()
{
    return productType;
}

Note that the relationship is defined as optional (and the column is nullable).

When doing HQL something like this

select p.name as col1, p.productType.name as col2 from ProductDfn p

An inner join is used to join ProductDfn to ProductType as hibernate generates the explicit SQL join from the implicit join in the select clause.

However when doing the above when productType is null (in the DB) no row is returned because of the inner join. For this relationship I would like to have hibernate default to doing an outer join (because the relationship is optional) so I would get a "null" back for col2 rather than no row at all.

Does anyone know if this is possible?

Thanks.

A: 

An inner join is used because you've explicitly listed p.productType.name in your select clause. This wouldn't have happened were you just to select ProductDfn since your fetch is set to LAZY.

If you only need to retrieve those two properties you'll have to explicitly specify an outer join in your query:

select p.name as col1, ptype.name as col2
  from ProductDfn p
  left join fetch p.productType ptype
ChssPly76
Thanks, I was looking for a way to have hibernate change it's default join type for implicit joins done in the select clause. The reason being is that we have an app where many queries are auto/semi generated due to retrieve tabular data.So we can do base object = ProductDfn and col1 is name, col2 is productType.name etc. Normally this works fine, just for nullable foreign keys it doesn't work quite the way I'd like.
Mike Q
There's no "default join type", though - implicit join will _always_ be inner. If your queries are being generated, consider using Criteria instead of HQL - it basically forces use to describe the association explicitly (via alias / nested criteria) and you can always specify it as outer join.
ChssPly76
A: 

Hi Mike Q,Have you find the way you'd like?I meet the same problem.

L.J.W