Despite the use of lazy fetch, Hibernate JPA OnToone is still inquiring

The problem I encountered is that Hibernate queries a class on the other side of the lazy onetoone relationship.

Query top_players (depending on cache settings) Query through the mapping table to obtain the ID of the QHPlayer table.

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

However, it is in two different scenarios I am doing something wrong in it.

If I turn on the cache, it will query the instance of QHPlayer, and then it will query to the inventory_item table.
If I turn off the cache, it will proceed to QHPlayer by adding inventory_item Query.

The problem is that no matter what I do, it insists on querying the inventory_item table. This is what I don’t want. I don’t need the data
currently in inventory_item.

I Suppose there is a problem with the onetoone statement between QHPlayer and PlayerInventoryItem.

Any ideas?

The relevant code is as follows:

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 results = query.getResultList();
< br />

@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;
}< br />
@OneToOne( mappedBy ="playerTemplate", fetch = FetchType.LAZY)
@XmlTransient
public PlayerInventoryItem getInventoryItem() {
return inventoryItem;
}

}


@Entity( name = "qhplayer_inventory_item" )
@DiscriminatorValue("PLAYER")
@Ca che(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() {< br /> return playerTemplate;
}
}

I found the answer

This is because one-to-one can be empty. Then it can’t achieve lazy loading of objects. So I solve it through a many-to-one relationship, just looking for a collection of objects.

p>

This is a good reference.
http://community.jboss.org/wiki/Someexplanationsonlazyloadingone-to-one

At the same time, thanks to anyone who reads this content.< /p>

The problem I have encountered is that Hibernate queries for a class on the other side of the lazy onetoone relationship.

The query for top_players (depending on Cache settings) through mapping Query the table to obtain the ID of the QHPlayer table.

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

However, it is in two different scenarios I did it wrong.

If I turn on the cache, it will query the instance of QHPlayer, and then it will query to the inventory_item table.
If I turn off the cache, it will query QHPlayer by adding inventory_item .

The problem is that no matter what I do, it insists on querying the inventory_item table. This is what I don’t want. I don’t need data
currently in inventory_item.

I assume QHPlayer and PlayerInventoryItem There is a problem with the onetoone statement.

Any thoughts?

The relevant code is as follows:

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 results = query.getResultList();
< br />

@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;
}< br />
@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() {< br /> return playerTemplate;
}
}

I found the answer.

This It’s because one to one can be empty. Then it can’t implement lazy loading of objects. So I solve it through a many-to-one relationship, just looking for a collection of objects.

This is a good reference .
http://community.jboss.org/wiki/Someexplanationsonlazyloadingone-to-one

Also thank anyone who reads this content.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4519 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.