views:

89

answers:

2
SQLQuery query = session.createSQLQuery("select {o.id} from order o " +
        "LEFT JOIN bookings b ON b.id = o.bookingId " +
);
List pusList = query.addEntity(Order.class)
        .list();

and in order class I have:

@OneToOne(cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.LAZY)
private Trip trip;

but during execution i see: fist, main sql. and many-many queries like this: Hibernate: /* load Trip */ select......

How can I to denny fetching Trips?

Update: Trip is useless in this case. And better is don't fetch it.

A: 

You are fetching lazily this means that the main will be loaded and then each trip as it is required.

In general see Hibernate performance documents to see what can be done.

As this is a one to one mapping I would change the fetch to fetch= FetchType.JOIN.This will load the trip at the same time as main and not add any more trips to the database.

Mark
Yes, but fetches of Trips are happened immediately after list() call.
Max
If you chnage the fetch type then they will be loaded at the same time as main. Lazy loading means they are done at the latest time which will be when you access them in your object
Mark
A: 

They way you've set this up is that in order to build your order object, the database has to look at the trip table (if I read your definitins correctly, then there is a column 'order' in the 'trip' table that references the order). Are you sure that this is really a one-to-one relationship? I don't know about your domain, but typically foreign keys map to a one-to-many relationship and the attribute would be a collection in trips or orders.

Please include some more info about your tables/relationships and the corresponding classes, especially the members that are supposed to contain the relationships. I would think that this gives us some more informmation to explain why the loading occurs and what can be done to avoid it.

IronGoofy