JPA Eclipselink错误:"字段' Target_id'没有默认值"

时间:2015-02-21 18:51:55

标签: mysql jpa eclipselink

我会尽量做到简短目标尽可能。我正在使用JPA Eclipselink 2.5.2。使用MySQL脚本创建数据库后,我正在尝试填充它。编译给我以下错误:

  

线程“main”中的异常javax.persistence.PersistenceException:   异常[EclipseLink-4002](Eclipse持久性服务 -   2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.DatabaseException内部   例外:java.sql.SQLException:字段'Target_id'没有   默认值错误代码:1364调用:INSERT INTO TARGET(32_bit_arch,   IP,Name,Operating_system)VALUES(?,?,?,?)bind => [真正,   127.0.0.1,Intel i7 2.6 Ghz + 8 GB,CentOS 6.5 32 bit]

我搜遍了SO和Google,但即使我发现类似情况(例如this one),解决方案似乎也不起作用(使用@JoinColumn删除非空约束(可空) = false)或appplying @OneToMany(orphanRemoval = true)等)。相关信息如下:

1。 MySQL表创建脚本:

-- -----------------------------------------------------
-- Table `fault_injector`.`Target`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `fault_injector`.`Target` ;

CREATE TABLE IF NOT EXISTS `fault_injector`.`Target` (
  `Target_id` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(30) NOT NULL,
  `32_bit_arch` TINYINT(1) NOT NULL,
  `IP` VARCHAR(15) NOT NULL,
  `Operating_system` VARCHAR(30) NOT NULL,
  PRIMARY KEY (`Target_id`),
  UNIQUE INDEX `idTarget_UNIQUE` (`Target_id` ASC),
  UNIQUE INDEX `IP_UNIQUE` (`IP` ASC))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `fault_injector`.`Experiment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `fault_injector`.`Experiment` ;

CREATE TABLE IF NOT EXISTS `fault_injector`.`Experiment` (
  `Exp_id` INT NOT NULL AUTO_INCREMENT,
  `Creator_id` INT NOT NULL,
  `Target_id` INT NOT NULL,
  `Name` VARCHAR(30) NOT NULL,
  `Creation_date` TIMESTAMP NOT NULL,
  `Injection_date` TIMESTAMP NULL,
  `Description` VARCHAR(300) NOT NULL,
  PRIMARY KEY (`Exp_id`),
  UNIQUE INDEX `Experiment_id_UNIQUE` (`Exp_id` ASC),
  INDEX `Target_id_idx` (`Target_id` ASC),
  INDEX `User_id_idx` (`Creator_id` ASC),
  CONSTRAINT `Experiment_User_id_fk`
    FOREIGN KEY (`Creator_id`)
    REFERENCES `fault_injector`.`User` (`User_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `Experiment_Target_id_fk`
    FOREIGN KEY (`Target_id`)
    REFERENCES `fault_injector`.`Target` (`Target_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

2。目标实体:

@Entity
@NamedQuery(name="Target.findAll", query="SELECT t FROM Target t")
public class Target implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="Target_id")
    private int target_id;

    [...]

    //bi-directional many-to-one association to Experiment
    @OneToMany(mappedBy="target")
    private List<Experiment> experiments;

    [...]

    public List<Experiment> getExperiments()
    {
        if(this.experiments==null)
            this.experiments = new ArrayList<Experiment>();

        return this.experiments;
    }

    public Experiment addExperiment(Experiment experiment) {
        getExperiments().add(experiment);
        experiment.setTarget(this);

        return experiment;
    }

第3。实验实体:

@Entity
@NamedQuery(name="Experiment.findAll", query="SELECT e FROM Experiment    e")
public class Experiment implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="Exp_id")
    private int exp_id;

    [...]

    //bi-directional many-to-one association to Target
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="Target_id")
    private Target target;

    [...]

    public void setTarget(Target target) {
        this.target = target;
    }

4。代码

虽然编译器说当实体管理器调用flush(em.flush();)时发生错误,但我知道其他行与错误有关,因为如果我发表评论错误就会消失:{{1 (请注意,目标和实验都是具有相同名称的实体的实例 - 如果需要,我可以发布更多代码,但是现在我试图让问题不那么长())。所以似乎问题在于Target和Experiment之间的双向关联,但除此之外,我还是坚持这个错误。

1 个答案:

答案 0 :(得分:1)

查看数据库中的表定义,例如

describe target

我猜,它与问题中显示的create table语句不同。特别是,列target_id的定义可能不会显示为auto_increment

根据target_id的定义,您可以使用

修改列
alter table target modify column target_id int not null auto_increment;