Spring Hibernate:从多对多关系中获取对象列表

时间:2018-12-14 01:31:37

标签: java mysql spring hibernate many-to-many

我的Spring-Boot应用程序中有两个实体,它们之间存在多对多关系。 我从特定的 Casopis 获取 tipoviPlacanja (TipPlacanja对象列表)时遇到问题。

Casopis.java

@Entity
public class Casopis {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    @Column
    private String merchantId;

    @Column
    private String merchantPassword;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "casopis")
    public Racun racun;

    @ManyToMany(cascade = { CascadeType.ALL })
    @JoinTable(
        name = "casopis_tipovi_placanja", 
        joinColumns = { @JoinColumn(name = "merchant_id") }, 
        inverseJoinColumns = { @JoinColumn(name = "tip_placanja_id") }
    )
    Set<TipPlacanja> tipoviPlacanja = new HashSet<>();


    public Casopis() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Casopis(String merchantId, String merchantPassword, Racun racun, Set<TipPlacanja> tipoviPlacanja) {
        super();
        this.merchantId = merchantId;
        this.merchantPassword = merchantPassword;
        this.racun = racun;
        this.tipoviPlacanja = tipoviPlacanja;
    }

    public Set<TipPlacanja> getTipoviPlacanja() {
        return tipoviPlacanja;
    }

    public void setTipoviPlacanja(Set<TipPlacanja> tipoviPlacanja) {
        this.tipoviPlacanja = tipoviPlacanja;
    }

}

TipoviPlacanja.java

@Entity
public class TipPlacanja {

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

    @Column
    private String naziv;

    @ManyToMany(mappedBy = "tipoviPlacanja")
    private Set<Casopis> casopisi = new HashSet<>();

    public TipPlacanja() {
    }

    public TipPlacanja(Long id, String naziv) {
        super();
        this.id = id;
        this.naziv = naziv;
    }

}

我的 data.sql 如下:

insert into tip_placanja(id, naziv) values (1, 'paypal');
insert into tip_placanja(id, naziv) values (2, 'kartica');
insert into tip_placanja(id, naziv) values (3, 'bitcoin');

insert into casopis(merchant_id, merchant_password) values ('22', 'bbbb');
insert into casopis(merchant_id, merchant_password) values ('11', 'aaaa');
insert into casopis_tipovi_placanja(merchant_id, tip_placanja_id) values ('11', 1);
insert into casopis_tipovi_placanja(merchant_id, tip_placanja_id) values ('11', 2);
insert into casopis_tipovi_placanja(merchant_id, tip_placanja_id) values ('11', 3);

insert into casopis_tipovi_placanja(merchant_id, tip_placanja_id) values ('22', 1);
insert into casopis_tipovi_placanja(merchant_id, tip_placanja_id) values ('22', 2);

我可以发誓它仅适用于

casopisRepository.findByMerchantId(id).getTipoviPlacanja();

但现在仅返回空列表。

我已经阅读了许多其他类似的问题,并且阅读了一些有关多对多的教程文章,但是什么也没有。

我尝试使用TipPlacanja存储库中的查询,但也没有执行任何操作。

@Query("SELECT tp FROM TipPlacanja tp JOIN tp.casopisi c WHERE c.merchantId = ?1")
    public Set<TipPlacanja> findBlaBla(String id);

我尝试直接在mySQL Workbench中键入查询,它工作正常,它从特定的 Casopis 返回所有 tipoviPlacanja 的ID。

select tp.id from tip_placanja tp
inner join casopis_tipovi_placanja ctp on tp.id = ctp.tip_placanja_id
inner join casopis c on ctp.merchant_id = c.merchant_id
where c.merchant_id = '22';

0 个答案:

没有答案