After a few search, I found some possible solution.
I would now need your help to know what is the best to implement.
So after reading
http://www.hibernate.org/315.html I can see that it is possible to have a OneToOne lazy by using optional=false.
So I tried it, but in my case it doesn't work...
Let's take a simple example, I have a VehicleEntity which has dependency to many other entities, VehicleStatusEntity, VehicleModelEntity, VehicleCharacteristicsEntity, etc... All these relations are @OneToOne.
So in my VehicleEntity I did:
Code:
@OneToOne(mappedBy = "vehicle", fetch = FetchType.LAZY, optional = false)
That line is the same on all the getCharacteristics(), getVehicleStatus(), ...
On the other side (on vehicleStatus, vehicleCharacteristics, etc) I have:
Code:
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "`vehicle_id`", nullable = false)
public VehicleEntity getVehicle() {
Then I create a query which retrieve a single VehicleEntity in my vehicleDAO, as I defined my OneToOne with optional=false it shouldn't load all the status,characteristics,model entities.
Code:
Criteria criteria = session
.createCriteria(VehicleEntity.class);
criteria.add(Restrictions.idEq(1));
VehicleEntity vehicle = (VehicleEntity) criteria
.uniqueResult();
Here is the generated query:
Quote:
/* criteria query */ select
this_."vehicle_id" as vehicle1_18_0_,
...
from
config."Vehicle" this_
where
this_."vehicle_id"=?
/* load model.VehicleCharacteristicsEntity */ select
vehiclecha0_."vehicleCharacteristics_id" as vehicleC1_21_1_,
...
vehicleent1_."vehicle_id" as vehicle1_18_0_,
...
from
config."VehicleCharacteristics" vehiclecha0_
inner join
config."Vehicle" vehicleent1_
on vehiclecha0_."vehicle_id"=vehicleent1_."vehicle_id"
where
vehiclecha0_."vehicle_id"=?
/* load model.VehicleModelEntity */ select
vehiclemod0_."vehicleModel_id" as vehicleM1_20_1_,
...
vehicleent1_."vehicle_id" as vehicle1_18_0_,
...
from
config."VehicleModel" vehiclemod0_
left outer join
config."Vehicle" vehicleent1_
on vehiclemod0_."vehicle_id"=vehicleent1_."vehicle_id"
where
vehiclemod0_."vehicle_id"=?
...
As you can see this can result as a performance nightmare...
So I would really need any feedback on how to really use this optional=false and why it doesn't work on my case;)
The possible way to solve these problems are:
1) Simulate a @ManyToOne with unique="true":
as described in the comment of
http://www.hibernate.org/162.html.
but I don't like this solution because @ManyToOne is simply not supposed to be a OneToOne...
2) Using a JoinColumn on both side of the relation and make it nullable="true":
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "`vehicleStatus_id`", nullable = true)
but I don't like this solution because I don't want to add a foreign key to all my tables.. When there are many relation and then a foreign key for each of them it will end with a lot of column on my table which i'd prefer to avoid...
3) Using a common JoinTable for all OneToOne association:
for example I could have the fields: vehicle_id, vehicleStatus_id, vehicleCharacteristics_id, etc.. in this table.
I think this can be a good solution but only when there are many relation on the table so sometimes I just don't want a join table and then I'm back to the original problem.
So here I would like to know what you think about these solution, and what would be best practise ?