0

I have this tables:

CREATE TABLE `t_user` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
..
}


CREATE TABLE `t_resto` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
..
}

CREATE TABLE `t_user_fav_resto` (
    `user_id`  bigint(20) DEFAULT NULL,
    `resto_id` bigint(20) DEFAULT NULL,
..
}

and

@Entity
@Table(name="t_user")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = {"id", "username"})
public class User implements Serializable, UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;


    @ManyToMany
    @JoinTable(name="t_user_fav_resto",
            joinColumns=@JoinColumn(name="user_id"),
            inverseJoinColumns=@JoinColumn(name="id"))
    @JsonIgnore
    private Set<MusicStyle> favoriteRestos = new HashSet<>();
..
}

and

@Entity
@Table(name="t_resto")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Resto implements Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;


    @ManyToMany
    @JoinTable(name="t_user_fav_resto",
            joinColumns=@JoinColumn(name="resto_id"),
            inverseJoinColumns=@JoinColumn(name="id"))
    @JsonIgnore
    private Set<User> users = new HashSet<>();
..
}

but I have this error runnig a query: Unknown column 'favoritest1_.id' in 'on clause'

select
    user0_.id as id1_5_,
    user0_.country_code as country_2_5_,
    user0_.creation_date as creation3_5_,
    user0_.email as email4_5_,
    user0_.enabled as enabled5_5_,
    user0_.last_login_date as last_log6_5_,
    user0_.last_password_reset_date as last_pas7_5_,
    user0_.latitude as latitude8_5_,
    user0_.longitude as longitud9_5_,
    user0_.mobile_os as mobile_10_5_,
    user0_.password as passwor11_5_,
    user0_.platform as platfor12_5_,
    user0_.target1 as target13_5_,
    user0_.target2 as target14_5_,
    user0_.target3 as target15_5_,
    user0_.username as usernam16_5_,
    user0_.validated as validat17_5_ 
from
    t_user user0_ 
left outer join
    t_user_fav_resto favoritest1_ 
        on user0_.id=favoritest1_.user_id 
left outer join
    t_resto resto2_ 
        on favoritest1_.id=resto2_.id 
where
    user0_.id=1559

the query is done automatically by the repo:

@Query("select u from User u left join u.favoriteRestos s where u.id = 1559 ")
    User findByIdWithFavoriteRestos (Long userId);
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301

1 Answers1

1

Your @ManyToMany mapping is wrong. You need to pass the names of the columns referencing this entity and the one you want to join:

@ManyToMany
@JoinTable(name="t_user_fav_resto",
        joinColumns=@JoinColumn(name="user_id"),
        inverseJoinColumns=@JoinColumn(name="resto_id")) // here goes the column name that joins child entity
@JsonIgnore
private Set<MusicStyle> favoriteRestos = new HashSet<>();
Andronicus
  • 25,419
  • 17
  • 47
  • 88