字段'ID'没有默认值

时间:2016-11-28 14:43:37

标签: java mysql hibernate

我正在使用MySQL与Hibernate和JPA2。

我有下表,我希望自动生成ID因为检查了AIAuto Increment):

enter image description here

CREATE TABLE `job` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `JOINING_DATE` double NOT NULL,
  `LASTACCESS_DATE` double NOT NULL,
  `PHONE_NUMBER` varchar(55) DEFAULT NULL,
  `MOBILE_NUMBER` varchar(55) DEFAULT NULL,
  `EMAIL_ADDRESS` varchar(55) DEFAULT NULL,
  `SEX` int(1) DEFAULT NULL,
  `JOB_TITLE` varchar(65) NOT NULL,
  `JOB_DESCRIPTION` varchar(800) NOT NULL,
  `JOB_DETAILS` varchar(800) DEFAULT NULL,
  `DRIVERS_LICENCE` int(1) DEFAULT NULL,
  `AVATAR` longblob,
  `TYPE` int(1) NOT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `ID_UNIQUE` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=87 DEFAULT CHARSET=utf8;

Job.java

@Entity
@Table(name = "job")
@XmlRootElement(name = "job")
public class Job extends AbstractDomain<Long> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    ...

Dao.java

protected T merge(T entity) throws InstantiationException, IllegalAccessException {
    T attached = null;
    if (entity.getId() != null) {
        attached = entityManager.find(entityClass, entity.getId());
    }
    if (attached == null) {
        attached = entityClass.newInstance(); // <= this is executed
    }

    BeanUtils.copyProperties(entity, attached);
    entityManager.merge(attached); // <= ID is null here

    return attached;
}

当我尝试merge表时,我收到以下错误:

  

Caused by: java.sql.SQLException: Field 'ID' doesn't have a default value

奇怪的是,这是在早期工作,并且Java代码没有改变,我确实只对其他一些数据库表进行了更改。我读过here有些人遇到了这个问题,并通过删除和导入他们的数据库来修复它。我确实放弃了架构并再次导入它。但仍然会得到同样的错误。

有人有任何想法吗?

0 个答案:

没有答案