I am using OneToOne between entity 'Product' and 'Photo'. One product can have no photos, or only one photo.
Mapping annotations:
Code:
public class Product{
...
private Photo photo;
public Product(String id, ... , Photo photo){
...
this.photo=photo;
}
@OneToOne
@JoinColumn(name="DEFAULT_PHOTO_ID")
public Photo getPhoto() {
return photo;
}
...
}
public class Photo{
...
}
As above, there's a constructor in Product which allows me to pass a Photo into it.
I tried to list some columns of ALL products whether each of them has a photo or not, so I used the constructor like this:
Query codeCode:
Query query = entityManager.createQuery("select new Product(id,...,photo) from Product");
return query.getResultList();
But what I got is a list containing rows only from the products having photos.
It seems that the products having no photos are ignored...
So I checked the SQL and found a unexpected 'inner join' in it:
The generated SQL (show_sql=true):Code:
select product0_.id as col_0_0_, ... product0_.DEFAULT_PHOTO_ID as col_8_0_ from PRODUCTS product0_ inner join PHOTOS photo1_ on product0_.DEFAULT_PHOTO_ID=photo1_.id
Now is the problem, maybe inner join is what Hibernate always does to a associated entity in constructor,
but is there a way I can use to list all entities with the customized contructor?
Thanks in advance.
Hibernate version:
3.2.1
Name and version of the database you are using:
Oracle 9i