I am using MySQL with Hibernate and JPA2.
I have the following table, which I would expect to auto generate the ID
because AI
(Auto Increment
) is checked:
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;
}
When I try merge
the table, I get the following error:
Caused by: java.sql.SQLException: Field 'ID' doesn't have a default value
Strange thing is this was working earlier, and the Java code has not changed, I did make changes to some other database tables only. I have read here that some people had this problem and fixed it by dropping and importing their database. I did drop the schema and imported it again. But still get the same error.
Does anyone have any ideas please?