3

I have an @Embeddable class:

@Embeddable
@Table(name="MY_TABLE")
public class MyTable {
    @ManyToOne(fetch=FetchType.LAZY, optional=false)
    @JoinColumn(name="my_other_id")
    private MyOtherEntity myOtherEntity;
    ...
}

@Entity
@Table(name="MY_OTHER_TABLE")
@DiscriminatorColumn(name="DISC", discriminatorType=DiscriminatorType.STRING)
public abstract class MyOtherEntity {
    ...
}

@Entity
@DiscriminatorValue("A")
public class MyOtherEntityA extends MyOtherEntity {
    ...
}

@Entity
@DiscriminatorValue("B")
public class MyOtherEntityB extends MyOtherEntity {
    ...
}

As with the LAZY fetch type, I would expect MyOtherEntity to be a proxy only, not the actual class initialised. But Hibernate is still issuing a query to fetch all its columns (except those further marked as LAZY within MyOtherEntity) once the owner class of MyTable calls getMyTable().

Adding optional=false as suggested in Hibernate: one-to-one lazy loading, optional = false and Hibernate ManyToOne FetchType.LAZY is not working? did not help. And the class is not final as suggested in Hibernate ManyToOne with FetchType.LAZY not fetching lazy . Any other reason how LAZY is not working as expected?

user1589188
  • 4,159
  • 12
  • 49
  • 99
  • how you are verifying that `FetchType.Lazy` is fetch data? – Nidhi257 Feb 12 '18 at 04:13
  • is it possible that you have some sort of internal implementation which requires hibernate to fetch the other entity despite it being lazy ? i remember we had issues like this when using lazy fetches, where the 'laziness' of attribute entities was only ensured for instances of the encapsulating entities which made no referencing during runtime to the internal lazy entities...... – Rann Lifshitz Feb 12 '18 at 04:20
  • `@Embeddable@Table` ...is maybe wished & possible, but i encounter this for the first time .... and `@DiscriminatorColumn` is (as per hibernate user guide) to be combined with `@Inheritance`... – xerx593 Feb 12 '18 at 04:56

1 Answers1

0

specify @Proxy(lazy=true)

@Proxy(lazy=true) 
@Embeddable
@Table(name="MY_TABLE")
Sibin
  • 358
  • 2
  • 10