-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Correct request but not correct results
PostPosted: Sat Aug 31, 2013 3:37 pm 
Newbie

Joined: Sat Aug 31, 2013 3:21 pm
Posts: 1
Hi everyone,

I'm using the play framework and I have several entities :

Code:
@Entity
public class Player extends Model {

    public enum Gender {

        MALE, FEMALE
    }

    @Required
    public String name;

    @Required
    public Gender gender;

    @Required
    public Long gold;
}

@Entity
public class Building extends Model {

    @Required
    @ManyToOne(fetch = FetchType.LAZY)
    public Player owner;

    @Required
    public String buildingType;

    @Required
    public Long buildingId;
}

@Entity
public class Stock extends Model {

    // Either a product type or a raw material
    @Required
    public Long goodId;

    @Required
    public Boolean isProduct;

    @Required
    public String image;
}

@Entity
public class Contract extends Model {

    @Required
    @ManyToOne(fetch = FetchType.LAZY)
    public Player player;

    @Required
    @ManyToOne(fetch = FetchType.LAZY)
    public Building building;

    @Required
    @ManyToOne(fetch = FetchType.LAZY)
    public Stock stock;

    @Required
    public Long ordersLeft;

    @Required
    public Long cyclesLeft;
}


What I would like to do is to retrieve all the Contracts with their associated Building, Stock and Player.

Here is the query :

Code:
public static List<Contract> retrieveContractsForNewOrder() {
        return find("select distinct c from Contract c "
                + "left join fetch c.player "
                + "left join fetch c.stock "
                + "left join fetch c.building "
                + "where c.cyclesLeft = 0 and c.ordersLeft > 0").fetch();
    }


The query seems correct, I'm retrieving the correct Contracts, the associated Building and Stock but not the associated Player.
For the player, the class is Player_$$_javassist_22 and it has a handler with this value : JavassistLazyInitializer.

That means that the Player has not been loaded via the query and will be loaded as soon as I will use it (via another query). But that's not what I want, I want it to be loaded via my previous query at the same time the Contracts and their associated Building and Stock are loaded.

I don't know why the Player is not loaded : players exist in database and everything is correct.

When I enable the log of the queries and the results I can see that the query is correct (the joins are correct and all the fields from all entities are in the select).
However the results are not correct : I have all the contracts with all the fields of the contract. I also have all fields of the Stock entity, but I have no field from Building and Player entities ...

What is wrong with the results ? Why am I not retrieving the Player and Building's fields ?

When I try to copy the query issued by Hibernate and execute it directly in MySQL I have correct results with all fields (from Contract, Building, Stock and Player).

Why is there a difference between results from Hibernate and results from MySQL ? (Hibernate is using the same MySQL database).

Thank you for your help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.