gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:10:27 25 4
gpt4 key购买 nike

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

我想用匹配的补品检索所有 Gin 。我的数据库创建语句是这样的:

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

我遇到这样的错误:

enter image description here

有人可以帮帮我吗?

编辑:

感谢 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) 时它可以使用延迟加载,但它会给我这个错误:

enter image description here

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

最佳答案

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

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

}

关于java - Ebean 与连接的多对多关系失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28779979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com