Home:ALL Converter>JPA loads too much data even if fetchtype is lazy

JPA loads too much data even if fetchtype is lazy

Ask Time:2018-06-08T21:54:54         Author:Francois

Json Formatter

I have an application that loads data related to a sewer network. Here is a sample of entities involved. First, I define a Network entity:

@Entity
@Table(name = "network")
public class Network extends Serializable {
  @Id
  @generatedValue(strategy=GenerationType.AUTO)
  private Long id;

  ...
  @OneToMany(mappedBy = "network")
  private List<Conduit> conduits;

  @OneToMany(mappedBy = "network")
  private List<ConduitVolumeter> conduitVolumeters;

  ...
}

Second entity involved is Conduit, which represents a pipe in the network.

@Entity
@Table(name = "conduit")
@NamedQuery(name = "Conduit.findAll", query = "SELECT c FROM Conduit c")
public class Conduit extends Serializable {
  @Id
  @generatedValue(strategy=GenerationType.AUTO)
  private Long id;

  ...
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "id_network", nullable = false)
  private Network network;

  @OneToOne(mappedBy = "conduit", fetch = FetchType.LAZY)
  private ConduitVolumeter conduitVolumeter;

  ...
}

Next, I define ConduitVolumeter. This represents a measuring device used to measure volume in a conduit. Since volumes can be measured in other network items, there is an abstract class Volumeter associated to the volumeter table in the database.

@Entity(name = "ConduitVolumeter")
@DiscriminatorValue("1")
public class ConduitVolumeter extends Volumeter {

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "id_nme")
  private Conduit conduit;

  ...
}

@Entity
@Table(name = "volumeter")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "nme_type", discriminatorType = DiscriminatorType.INTEGER)
public abstract class Volumeter extends Serializable {
  @Id
  @generatedValue(strategy=GenerationType.AUTO)
  private Long id;

  ...
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "id_network", nullable = false)
  private Network network;

  ...
}

The problem is that when I try to load all conduits associated to a network, Hibernate tries systematically to load every volumeter associated with each loaded conduit, one at a time, according to its logs.

Hibernate: 
    select
        conduit0_.id as id_1_7_,
        conduit0_.name as name23_7_,
        conduit0_.id_network as id_netw39_7_,
    from
        conduit conduit0_ cross 
    join
        network network1_ 
    where
        conduit0_.id_network=?

...

Hibernate: 
    select
        conduitvol0_.id as id2_116_0_,
        conduitvol0_.name as name10_116_0_,
        conduitvol0_.id_network as id_netw15_116_0_,
        conduitvol0_.id_nme as id_nme17_116_0_ 
    from
        volumeter conduitvol0_ 
    where
        conduitvol0_.id_nme=? 
        and conduitvol0_.nme_type=1

My question : why does Hibernate try to load volumeter data? Each @ManyToOne and @OneToOne annotations is set with FetchType.LAZY. What did I miss?

BTW, I'm asked to use JPA with a legacy database. Is the database model the problem? Is there a way to load the conduits without their volumeters?

Regards.

Francois

Author:Francois,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762240/jpa-loads-too-much-data-even-if-fetchtype-is-lazy
yy