JayZee
JayZee

Reputation: 860

JPA query left join without loading the joined entity field

I have 3 JPA entities such as:

@Entity
public class Link implements  {

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "network", referencedColumnName = "id")
   private Network network;
   //...
}

@Entity
public class Network implements LinkOwner {
    @OneToMany(mappedBy = "network")
    @Cascade(value = { CascadeType.ALL })
    private Set<Link>   links;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project", referencedColumnName = "id", nullable = false)
     private Project Network.project;
  //...
 }


@Entity
public class Project {
    @OneToMany(mappedBy = "project", orphanRemoval = true)
    @Cascade(value = { CascadeType.ALL })
    @Fetch(FetchMode.SELECT)
    private Set<Network>    networks;
 }

And I do a JPA query such as:

SELECT l FROM Link l left join fetch l.subnetwork sann   
where sann.project.id = :projectId 

and it generates a SQL query similar to:

select * from RMT6.link, SUBNETWORK where link.subnetwork = SUBNETWORK.id 
and SUBNETWORK.project=?

How can I trigger a JPQL query that selects only the fields of the first entity and exclude those of the second one? What do I need to change in my JPQL query?

Upvotes: 0

Views: 3153

Answers (1)

Zaw Than oo
Zaw Than oo

Reputation: 9935

Base on your entity relationship, you don't need to use JOIN query, I think.

SELECT * FROM LINK l WHERE l.network.project.id = :projectId

Upvotes: 1

Related Questions