0

i have a simple database one-directionnel-relationthip :

public class Bill{

@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn( name="BILL_ACTOR" )
    private fr.teamwill.cbm.model.actor.Actor actor;
}

i need to get the email of the actor, but when i do this i get an exception about lazy:

bill.getActor().getEmail() ---> exception

PLease can you give me sql requete to get the actor , i am a bit weak in database , here is wat i think of:

select a.email  From Bill b , Actor a fetch  join  ?????? or inner join?? 

Please give me the requete and thank you alot

1
  • you can enable hibernate sql trace and see the sql log4j.logger.org.hibernate.SQL=DEBUG will enable the sql trace. Commented Mar 9, 2020 at 13:47

1 Answer 1

1

You cannot get the actor data because you've set it as a LAZY fetch type.

For your requested query, it can be

SELECT a.email  
FROM Bill b, Actor a 
WHERE b.BILL_ACTOR = a.id

I am curious why you're setting the LAZY fetch type for this attribute.

As usual, LAZY mode should be applied for heavily loading data. In your case, it should be applied for bills attribute in the Actor class. Here is an example

public class Bill {
  @ManyToOne
  @JoinColumn( name="BILL_ACTOR" )
  private fr.teamwill.cbm.model.actor.Actor actor;
}

public class Actor {
   //...
   @OneToMany(fetch=FetchType.LAZY)
   private List<Bill> bills

}
Sign up to request clarification or add additional context in comments.

2 Comments

Marking ManyToOnes as lazy fetched it is a viable strategy if there are going to be thousands of instances of the parent entity fetched and the children are only rarely actually referenced.
It was clearly mentioned that it is uni-directional relationship. So why do you put bills in Actor class? @Tran Ho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.