NetBeans 中的“无主键”错误

Posted

技术标签:

【中文标题】NetBeans 中的“无主键”错误【英文标题】:"No primary keys" error in NetBeans 【发布时间】:2011-08-24 10:06:00 【问题描述】:

在 NetBeans7 中,当我尝试添加“来自数据库的实体类”时,会出现以下问题:左侧的某些表显示为灰色并标记为 “(无主键)”,所以我不能添加它们。 实际上,主键是正确的。表格groups、groupsyear、specialization 是灰色的,其他都很好。

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`Faculty`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Faculty` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Faculty` (
  `idFaculty` INT NOT NULL ,
  `name` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`idFaculty`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Specialization`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Specialization` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Specialization` (
  `idSpecialization` INT NOT NULL ,
  `Faculty_idFaculty` INT NOT NULL ,
  `Name` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`idSpecialization`) ,
  CONSTRAINT `fk_Specialization_Faculty`
    FOREIGN KEY (`Faculty_idFaculty` )
    REFERENCES `mydb`.`Faculty` (`idFaculty` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_Specialization_Faculty` ON `mydb`.`Specialization` (`Faculty_idFaculty` ASC) ;


-- -----------------------------------------------------
-- Table `mydb`.`Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Student` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Student` (
  `idStudent` INT NOT NULL ,
  `Specialization_idSpecialization` INT NOT NULL ,
  `Name` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`idStudent`) ,
  CONSTRAINT `fk_Student_Specialization1`
    FOREIGN KEY (`Specialization_idSpecialization` )
    REFERENCES `mydb`.`Specialization` (`idSpecialization` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_Student_Specialization1` ON `mydb`.`Student` (`Specialization_idSpecialization` ASC) ;


-- -----------------------------------------------------
-- Table `mydb`.`Discipline`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Discipline` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Discipline` (
  `idDiscipline` INT NOT NULL ,
  `Name` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`idDiscipline`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Lecturer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Lecturer` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Lecturer` (
  `idLecturer` INT NOT NULL ,
  `Name` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`idLecturer`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`GroupsYear`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`GroupsYear` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`GroupsYear` (
  `idGroupsYear` INT NOT NULL ,
  `Discipline_idDiscipline` INT NOT NULL ,
  `Lecturer_idLecturer` INT NOT NULL ,
  `Year` YEAR NOT NULL ,
  PRIMARY KEY (`idGroupsYear`) ,
  CONSTRAINT `fk_Groups_Year_Discipline1`
    FOREIGN KEY (`Discipline_idDiscipline` )
    REFERENCES `mydb`.`Discipline` (`idDiscipline` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_GroupsYear_Lecturer1`
    FOREIGN KEY (`Lecturer_idLecturer` )
    REFERENCES `mydb`.`Lecturer` (`idLecturer` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_Groups_Year_Discipline1` ON `mydb`.`GroupsYear` (`Discipline_idDiscipline` ASC) ;

CREATE INDEX `fk_GroupsYear_Lecturer1` ON `mydb`.`GroupsYear` (`Lecturer_idLecturer` ASC) ;


-- -----------------------------------------------------
-- Table `mydb`.`Groups`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Groups` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Groups` (
  `idGroups` INT NOT NULL ,
  `GroupsYear_idGroupsYear` INT NOT NULL ,
  `Lecturer_idLecturer` INT NOT NULL ,
  PRIMARY KEY (`idGroups`) ,
  CONSTRAINT `fk_Group_GroupsYear1`
    FOREIGN KEY (`GroupsYear_idGroupsYear` )
    REFERENCES `mydb`.`GroupsYear` (`idGroupsYear` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Group_Lecturer1`
    FOREIGN KEY (`Lecturer_idLecturer` )
    REFERENCES `mydb`.`Lecturer` (`idLecturer` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_Group_GroupsYear1` ON `mydb`.`Groups` (`GroupsYear_idGroupsYear` ASC) ;

CREATE INDEX `fk_Group_Lecturer1` ON `mydb`.`Groups` (`Lecturer_idLecturer` ASC) ;


-- -----------------------------------------------------
-- Table `mydb`.`Current_Group_has_Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Current_Group_has_Student` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Current_Group_has_Student` (
  `Groups_idGroups` INT NOT NULL ,
  `Student_idStudent` INT NOT NULL ,
  PRIMARY KEY (`Groups_idGroups`, `Student_idStudent`) ,
  CONSTRAINT `fk_Groups_has_Student_Groups1`
    FOREIGN KEY (`Groups_idGroups` )
    REFERENCES `mydb`.`Groups` (`idGroups` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Groups_has_Student_Student1`
    FOREIGN KEY (`Student_idStudent` )
    REFERENCES `mydb`.`Student` (`idStudent` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_Groups_has_Student_Student1` ON `mydb`.`Current_Group_has_Student` (`Student_idStudent` ASC) ;

CREATE INDEX `fk_Groups_has_Student_Groups1` ON `mydb`.`Current_Group_has_Student` (`Groups_idGroups` ASC) ;


-- -----------------------------------------------------
-- Table `mydb`.`LongTimeAgo_Group_has_Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`LongTimeAgo_Group_has_Student` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`LongTimeAgo_Group_has_Student` (
  `Groups_idGroups` INT NOT NULL ,
  `Student_idStudent` INT NOT NULL ,
  PRIMARY KEY (`Groups_idGroups`, `Student_idStudent`) ,
  CONSTRAINT `fk_Groups_has_Student_Groups2`
    FOREIGN KEY (`Groups_idGroups` )
    REFERENCES `mydb`.`Groups` (`idGroups` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Groups_has_Student_Student2`
    FOREIGN KEY (`Student_idStudent` )
    REFERENCES `mydb`.`Student` (`idStudent` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_Groups_has_Student_Student2` ON `mydb`.`LongTimeAgo_Group_has_Student` (`Student_idStudent` ASC) ;

CREATE INDEX `fk_Groups_has_Student_Groups2` ON `mydb`.`LongTimeAgo_Group_has_Student` (`Groups_idGroups` ASC) ;


-- -----------------------------------------------------
-- Table `mydb`.`Past_Group_has_Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Past_Group_has_Student` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Past_Group_has_Student` (
  `Groups_idGroups` INT NOT NULL ,
  `Student_idStudent` INT NOT NULL ,
  PRIMARY KEY (`Groups_idGroups`, `Student_idStudent`) ,
  CONSTRAINT `fk_Groups_has_Student_Groups3`
    FOREIGN KEY (`Groups_idGroups` )
    REFERENCES `mydb`.`Groups` (`idGroups` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Groups_has_Student_Student3`
    FOREIGN KEY (`Student_idStudent` )
    REFERENCES `mydb`.`Student` (`idStudent` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_Groups_has_Student_Student3` ON `mydb`.`Past_Group_has_Student` (`Student_idStudent` ASC) ;

CREATE INDEX `fk_Groups_has_Student_Groups3` ON `mydb`.`Past_Group_has_Student` (`Groups_idGroups` ASC) ;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

为了更好的理解:http://i.stack.imgur.com/cGsjG.png

我希望有人会发现架构中的错误。

【问题讨论】:

你想为hibernate生成java类吗? 【参考方案1】:

我找到了答案。也许不是最好的,但感谢这个链接 http://aquaryus.wordpress.com/2011/04/11/tutorial-create-a-quick-jee-web-service-with-mysql-and-netbeans-glassfish/ 我刚刚用 MyISAM 替换了引擎类型 InnoDB,现在一切正常! (至少它允许为 JPA 创建正确的 POJO)但是为什么 InnoDB 不起作用的问题仍然存在。

【讨论】:

救生员 +100。我确认这有效。尽管链接似乎已损坏,但我将“Engine=InnoDB”更改为“Engine=MyISAM”,现在一切正常!

以上是关于NetBeans 中的“无主键”错误的主要内容,如果未能解决你的问题,请参考以下文章

如何修复netbeans中的java语言级别错误

Java项目中的Netbeans构建错误

Netbeans 错误:架构不存在

使用 Netbeans 时 java 中的初始化程序错误异常

jsf的xthml文件中的javascript中的不等号(“<”)引发错误(在netbeans中)[重复]

插入数据库时​​netbeans项目中的参数类型不匹配