我有一些Gins,而且我有一些与多种关系有关的补品。现在我也有桌子gin2tonic。它只有两个键,这些键对于来自补品和杜松子酒的id都是陌生的。
我想用匹配的补品检索所有Gins。我的db create语句是这样的:
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(当我错误时纠正我)
答案 0 :(得分:3)
在两个具有多对多关系的实体中都有相同类型的列表变量。在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
);
}