0

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:

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;
}

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?

Community
  • 1
  • 1
Richard
  • 8,193
  • 28
  • 107
  • 228
  • I see you're using MySQL, can you right click on the table on the left, copy to clipboard, create statement and paste the content here? – Tchopane Nov 28 '16 at 15:10
  • @Tchopane, thanks for the reply. Just added the `CREATE STATEMENT` to the description. – Richard Nov 28 '16 at 15:13
  • Ok, maybe you should remove that unique constraint on the primary key. Being the primary key already makes this column's values unique. – Tchopane Nov 28 '16 at 15:22
  • And maybe you changed some tables using some of the columns of 'job' as foreign keys, you should check that everything is defined properly in the others tables. – Tchopane Nov 28 '16 at 15:29
  • @Tchopane, thanks I will try that. Also, I noticed it may be complaining about the FK tables ID that is not Auto Increment. Just checking that too. – Richard Nov 28 '16 at 15:30
  • Yes, that was my mistake. It was not the Job table that had the error, it was a joined table that had ID as null, and it wasn't Auto Incremented. As soon as I set it to AI, it works. Appreciate your help. – Richard Nov 28 '16 at 15:37
  • Nice to hear that it works now! – Tchopane Nov 28 '16 at 15:39

0 Answers0