【问题标题】:@ManyToMany in Spring data jpaSpring数据jpa中的@ManyToMany
【发布时间】:2021-11-12 20:11:23
【问题描述】:

我有这张桌子:

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<>();
..
}

但我在运行查询时遇到此错误: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

查询由 repo 自动完成:

@Query("select u from User u left join u.favoriteRestos s where u.id = 1559 ")
    User findByIdWithFavoriteRestos (Long userId);

【问题讨论】:

  • 如果您觉得答案有帮助,请考虑投票:>

标签: mysql spring spring-boot jpa spring-data-jpa


【解决方案1】:

您的@ManyToMany 映射错误。您需要传递引用此实体的列的名称以及要加入的列的名称:

@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<>();

【讨论】:

猜你喜欢
  • 2015-12-11
  • 2021-03-03
  • 2017-02-02
  • 2020-02-04
  • 2016-07-12
  • 2017-12-12
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多