Ebean 与连接的多对多关系失败

Ebean many to many relationship with join fails

我喝了一些杜松子酒和一些补品,这是多对多的关系。现在我还有 table gin2tonic。它只有 2 个键,它们都与 tonic 和 gin 的 id 无关。

我想用匹配的补品取回所有杜松子酒。我的数据库创建语句是这样的:

CREATE TABLE `tonic` (
  `idTonic` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `type` varchar(360) DEFAULT NULL,
  `ingredient` varchar(1440) DEFAULT NULL,
  `origin` varchar(1440) DEFAULT NULL,
  `picture_link` varchar(360) DEFAULT NULL,
  `aroma` varchar(360) DEFAULT NULL,
  PRIMARY KEY (`idTonic`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;


CREATE TABLE `gin` (
  `idGin` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `picture_link` varchar(360) DEFAULT NULL,
  `origin` varchar(1440) DEFAULT NULL,
  `ingredient` varchar(1440) DEFAULT NULL,
  `aroma` varchar(360) DEFAULT NULL,
  `alc_percentage` double DEFAULT NULL,
  `type` varchar(360) DEFAULT NULL,
  PRIMARY KEY (`idGin`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

CREATE TABLE `gin2tonic` (
  `id_gin` int(11) DEFAULT NULL,
  `id_tonic` int(11) DEFAULT NULL,
  KEY `idGin_idx` (`id_gin`),
  KEY `idTonic_idx` (`id_tonic`),
  CONSTRAINT `fk_gin2tonic_idGin` FOREIGN KEY (`id_gin`) REFERENCES `gin` (`idGin`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_gin2tonic_idTonic` FOREIGN KEY (`id_tonic`) REFERENCES `tonic` (`idTonic`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在我的 java 类 中,我认为我可以这样做:

@Entity
public class Gin extends Model {
    @Id
    @Column(name="idGin")
    private Integer idGin;
    private String aroma;
    ...// some other vars from the database not important

    @ManyToMany
    @JoinTable(name="gin2tonic")
    private List<Tonic> tonics;

        public static Finder<Integer, Gin> find = new Finder<>(
            Integer.class, Gin.class
    );
}


@Entity
public class Tonic extends Model {
    @Id
    @Column(name="idTonic")
    private Integer idTonic;

    private String aroma;

    // some other vars from the database not important


    @ManyToMany(mappedBy = "tonics")
    public List<Tonic> tonics;

    public static Finder<Integer, Tonic> find = new Finder<>(
            Integer.class, Tonic.class
    );

}

然后我这样执行:

 List<Gin> gins = Gin.find.all();

我遇到的错误是:

有人可以帮我吗?

编辑:

感谢 singhakash,错误已解决,但是,当我想像这样打印列表时,我现在正在延迟 BeanList:

List<Gin> gins = Gin.find.all();
for(Gin x : gins){
    System.out.println("idGin: " + x.getIdGin());
    System.out.println("Tonics: "+ x.getTonics());
    System.out.println("---------------------");
}

编辑:

我知道它在我执行 x.getTonics.get(0) 时可以使用延迟加载,它给了我这个错误:

据我所知,查询是否错误,因为他不知道gin2tonic中的列是id_gin而不是idGin(错误时纠正我)

您在具有多对多关系的两个实体中都有相同类型的列表变量。在 Tonic class 中将列表类型更改为 Gin .Do

@Entity
public class Gin extends Model {
    @Id
    @Column(name="idGin")
    private Integer idGin;
    private String aroma;
    ...// some other vars from the database not important

    @ManyToMany
    @JoinTable(name="gin2tonic")
    private List<Tonic> tonics;

        public static Finder<Integer, Gin> find = new Finder<>(
            Integer.class, Gin.class
    );
}


@Entity
public class Tonic extends Model {
    @Id
    @Column(name="idTonic")
    private Integer idTonic;

    private String aroma;

    // some other vars from the database not important


    @ManyToMany(mappedBy = "tonics")
    public List<Gin> gins;                    //changed to Gin type

    public static Finder<Integer, Tonic> find = new Finder<>(
            Integer.class, Tonic.class
    );

}