0

Just need a quick Help from you all. I have used mappedBy Instead of @JoinColumn just for performance Optimization:

@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="user")

I have been advised to set fetch=FetchType.EAGER. But I don't want to do that. Is there any other way to get lazily Loaded Collection Objects. Or should I use @JoinColumn for that? And mappedBy is then how useful for other if not able to read it lazily.

Sample entities

@Entity
@Table(name="users,uniqueConstraints=@UniqueConstraint(columnNames= "username"))
public class Users implements java.io.Serializable {
   private Set<IDPMaster> idpMaster;
    public void setIdpMaster(Set<IDPMaster> idpMaster) {
        this.idpMaster = idpMaster;
    }
    @OneToMany(cascade = CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="user")
    public Set<IDPUserManagerMapping> getIdpMapping() {
        return idpMapping;
    }
    //Getters and Setters 
    }


@Entity
@Table(name="IDP_Master",schema="eb")
public class IDPMaster implements Serializable {
    private Users idpUsers;
        @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="paticipant_id",nullable=false)
    public Users getIdpUsers() {
        return idpUsers;
    }
    public void setIdpUsers(Users idpUsers) {
        this.idpUsers = idpUsers;
    }
    }
alexbt
  • 16,415
  • 6
  • 78
  • 87
ThinkTank
  • 1,737
  • 5
  • 22
  • 36

1 Answers1

3

mappedBy can be used only in one entity i.e. the Parent entity whereas @JoinColumn can be used on the both side of the relationship. See JPA JoinColumn vs mappedBy for difference. Can you show the sample entities that you have created.

Also you need not to specify fetch=FetchType.LAZY because by default the fetchType is always lazy in Hibernate. In JPA however the fetch type is different for different mappings.

Edited.. The field name provided in the mappedBy should be "idpUsers" instead of "users". It should match the variable name in the child Entity.