简体   繁体   中英

Spring Data, fetching rows ManyToOne / OneToMany relationships?

I am working on a Spring project using Spring data. I have the following models :

Location.java

@Entity
@Table(name = "location")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Location {

    @Id
    @GeneratedValue
    private int id;

    private String latitude;

    private String longitude;

    private String date;

    @ManyToOne
    @JoinColumn(name ="user")
    @JsonBackReference
    private User user;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    ...
    ...
}

User.java

@Entity
@Table(name = "users")
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {

    @Id
    @GeneratedValue
    private int uid;
    private String first_name;
    private String last_name;
    private String email;
    private String password;
    private boolean enabled;
    private String avatar;

    @Enumerated(EnumType.STRING)
    @Column(name = "user_role")
    private Role role;

    @OneToMany(mappedBy="user")
    @JsonManagedReference
    private List<Location> locations;

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public List<Location> getLocations() {
        return locations;
    }

    public void setLocations(List<Location> locations) {
        this.locations = locations;
    }

}

Database

CREATE TABLE location (
id integer NOT NULL,
"user" integer,
...
CONSTRAINT location_pkey PRIMARY KEY (id),
CONSTRAINT location_user_fkey FOREIGN KEY ("user")
    REFERENCES users (uid) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION
);

CREATE TABLE users
(
uid integer NOT NULL,
...
CONSTRAINT users_pkey PRIMARY KEY (uid)
)

Goal Using a CrudRepository, i want to fetch a list of all Locations WITH or WITHOUT corresponsing users !

public interface LocationRepository extends CrudRepository<Location, Integer> {

    List<Location> findAll();

    @Query("SELECT l FROM Location l")
    List<Location> findLast();

}

So as i understand the above Query will get all the Locations without the corresponding users, but the following

@Query("SELECT l FROM Location l JOIN l.user u")

will get all the locations WITH the corresponding users

Problem: Both queries get all the Locations with the corresonding Users !

What am i doing wrong here ? Thanks

The problem is that you're mapping the Location to always retrieve User (and vice versa). If you don't want this behavior, you can mark to fetch the User in lazy way using FetchType.LAZY :

@ManyToOne(fetch=FetchType.LAZY, optional=false)
@JoinColumn(name ="user")
@JsonBackReference
private User user;

This way, User user in Location will only be retrieved when being accessing through the getter and if it is inside a valid session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM