我正在尝试在播放框架中使用 JPA(已启用进化)创建具有额外列的多对多关系,并且使用 JavaJpa hibernate-entitymanager 5.0.5 final 播放版本为 2.4.6。
这是我创建所有表的 sql 脚本:
CREATE TABLE IF NOT EXISTS `user_level` (
`level_id` bigint(20) NOT NULL AUTO_INCREMENT,
`level` int(3),
`level_name` varchar(255) NOT NULL,
PRIMARY KEY (`level_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `menu` (
`menu_id` bigint(20) NOT NULL AUTO_INCREMENT,
`menu_name` varchar(255),
`parent` bigint(20),
PRIMARY KEY (`menu_id`),
CONSTRAINT `FK_PARENT_MENU` FOREIGN KEY (`parent`) REFERENCES `menu` (`menu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `menu_level` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`level_id` bigint(20) NOT NULL,
`menu_id` bigint(20) NOT NULL,
`privilege` int(1) DEFAULT '0',
PRIMARY KEY (`id`),
CONSTRAINT `FK_MENU_LEVEL_USER_LEVEL_ID` FOREIGN KEY (`level_id`) REFERENCES `user_level` (`level_id`) ON UPDATE CASCADE,
CONSTRAINT `FK_MENU_LEVEL_MENU_ID` FOREIGN KEY (`menu_id`) REFERENCES `menu` (`menu_id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
下面是我的实体类
用户级别.java
@Entity
@Table(name = "user_level")
public class UserLevel {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "level_id")
private Long levelId;
@Column(name = "level")
private Integer level;
@Column(name = "level_name")
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userLevel", cascade = CascadeType.ALL)
private List<MenuLevel> menuLevels;
}
菜单.java
@Entity
@Table(name="menu")
public class Menu {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "menu_id")
private Long menuId;
@Column(name = "menu_name")
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "menu", cascade = CascadeType.ALL)
private List<MenuLevel> menuLevels;
/*parent child menu relations*/
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
private List<Menu> childMenu = new ArrayList<>();
@JoinColumn(name="parent", referencedColumnName = "menu_id")
@ManyToOne(fetch = FetchType.LAZY)
private Menu parent;
}
MenuLevel.java
@Entity
@Table(name = "menu_level")
public class MenuLevel {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public Long id;
@JoinColumn(name = "level_id", referencedColumnName = "level_id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private UserLevel userLevel;
@JoinColumn(name = "menu_id", referencedColumnName = "menu_id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Menu menu;
@Column(name = "privilege")
private Integer privilege;
}
当我运行游戏时,据说
引起:org.hibernate.AnnotationException:models.MenuLevel.menu 的referencedColumnNames(menu_id) 引用models.Menu 未映射到单个属性
此时我遇到了这个问题,如果有人能指出我的错误,我很高兴,因为我已经遵循了一些关于 JPA 多对多关系的教程和说明,但仍然无法弄清楚我哪里做错了。