Hibernate JPA OneToOne Requests Despite Lazy Fetch

I'm having problems when Hibernate requests a class on the other side of the lazy onetoone relationship.

The top_players query, depending on the cache settings, executes the query through the mapping table to get identifiers for the QHPlayer table.

After executing the main query, it queries each instance of the QHPlayer table.

However, this does not occur in two different scenarios.

If I turn on caching, it will query for QHPlayer instances, and then it will query the information in the inventory_item table. If I disable caching, it will make a request to QHPlayer with attachment to inventory_item.

The problem is that no matter how I do it, it insists on querying the inventory_item table. I don’t want that. I do not need the data in inventory_item at present.

I assume that something is wrong in my onetoone ads between QHPlayer and PlayerInventoryItem.

Any ideas please?

The corresponding code is given below:

    Query query = entityManager.createQuery( "SELECT c FROM top_players c WHERE c.teamId=:teamId ORDER BY c.level DESC, c.adjustedFantasyPointsTotal DESC, c.id ASC" );
    query.setParameter( "teamId", teamId );
    List<TopPlayers> results = query.getResultList();



    @XmlAccessorType( XmlAccessType.PROPERTY)
@Entity( name="player_template")
@Table( name="player_template" )
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class QhPlayer implements Serializable {

    @Id
    public Integer getPlayerTemplateId() {
        return playerTemplateId;
    }

    @OneToOne( mappedBy ="playerTemplate", fetch = FetchType.LAZY)
    @XmlTransient
    public PlayerInventoryItem getInventoryItem() {
        return inventoryItem;
    }

}


@Entity( name = "qhplayer_inventory_item" )
@DiscriminatorValue("PLAYER")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class PlayerInventoryItem extends InventoryItem {
    private QhPlayer playerTemplate;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="player_template_id")
    @XmlTransient
    public QhPlayer getPlayerTemplate() {
        return playerTemplate;
    }
}



@Entity( name="inventory_item" )
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name = "inventory_item_type",
        discriminatorType = DiscriminatorType.STRING
)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public abstract class InventoryItem {
    private int inventoryItemId;
}



@Entity( name = "top_players")
@XmlRootElement(name = "top_player")
@Table(name="player")
@SecondaryTables({
        @SecondaryTable(name="player_stats", pkJoinColumns={
                @PrimaryKeyJoinColumn(name="playerId", referencedColumnName="id")
        }),
        @SecondaryTable(name="player_mapping", pkJoinColumns={
                @PrimaryKeyJoinColumn(name="playerId", referencedColumnName="id")
        })       
})
@XmlAccessorType( XmlAccessType.PROPERTY )
public class TopPlayers {

    private QhPlayer playerTemplate;

    @XmlTransient
    @ManyToOne
    @JoinColumn( table="player_mapping", name = "player_template_id", nullable = true )
    public QhPlayer getPlayerTemplate() {
        return playerTemplate;
    }
}
+5
source share
1 answer

I have found the answer.

This is because a one-to-one NULL value. Then it cannot implement the lazy loading object. So I solved this by going over to a one-to-one relationship and just looking for a set of one object.

Here is a good link.
http://community.jboss.org/wiki/Someexplanationsonlazyloadingone-to-one

Thanks to those who read this.

+13
source

All Articles