user1310388
user1310388

Reputation:

org.hibernate.exception.GenericJDBCException: Field 'id' doesn't have a default value

I have this table in some application, with a generator specified and a Generationtype.AUTO, but whenever I try to persist it, I get an error from the server saying id does not have a default value. This is running on a jboss server AS7.x and the database engine is mysql.

So far I have no conclusion as to why does it happen. Can anyone help?

@SequenceGenerator(name = "id", sequenceName = "some_set_seq", initialValue = 1, allocationSize = 1)
@Table(name = "some_set")
public class SomeSet implements Serializable {

private static final long serialVersionUID = 633453570945334348L;
private long id;
private String name;
private String description;
private List<SomeItem> items;
private List<SomeEntity> someEntities;
private Date lastModification;

public SomeSet() {}

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "id")
  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  @Column(name = "name")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Column(name = "description")
  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  @OneToMany(cascade = CascadeType.ALL)
  @LazyCollection(LazyCollectionOption.FALSE)
  @JoinColumn(name = "some_set_id")
  @ForeignKey(name="FK_someItem_someSet")
  public List<SomeItem> getItems() {
    return items;
  }

  public void setItems(List<SomeItem> items) {
    this.items = items;
  }

  @ManyToMany(mappedBy = "someSets", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
  @ForeignKey(name="FK_someLink_someSet")
  public List<SomeEntity> getEntities() {
    return someEntities;
  }

  public void setEntities(List<SomeEntity> someEntities) {
    this.someEntities = someEntities;
  }

  @EntityProperty(name = "lastmodificationdate")
  @Column(name = "last_modification_date")
  public Date getLastModification() {
    return lastModification;
  }

  public void setLastModification(Date lastModification) {
    this.lastModification = lastModification;
  }
}

Upvotes: 0

Views: 5476

Answers (3)

Ankur jain
Ankur jain

Reputation: 993

I faced the same issue, and found the primary key in the table was not marked with Auto Increment. Please make the id field auto increment and resolve your issue.

Upvotes: 0

user1310388
user1310388

Reputation:

The database is created by some script implemented separately, not by hibernate. We found that in some other version of the software this table had autoincrement defined for the id which now was failing.

Putting it back made it work fine.

Upvotes: 0

Balaji Reddy
Balaji Reddy

Reputation: 5700

This is very generic exception and straight forward one. make sure your data model has property marked with default value to tat specific column.. Tis is mismatch between your data model and your domain model... Kindly check your schema/table and make changes accordingly..

Cheers!

Upvotes: 1

Related Questions