0

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;
  }
}
1
  • Try: @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="id") Commented Nov 19, 2013 at 16:07

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

3 Comments

can you post your scheme here?
This has been changed because I cannot show the real code or table, so I somehow renamed a few things and deleted others... But I did have a look at the db table and I see that | Field | Type | Null | Key | Default | Extra | | id | bigint(20) | NO | PRI | NULL | | So... although this field is not nullable and it is a primary key, it does have null as a default value. I will try to set it to 0 or 1 and see how that works
can you change your id generation strategy and check? Something like @GeneratedValue(strategy = GenerationType.AUTO/SEQUENCE/NATIVE)... Also make sure the strategy has been supported by your database engine
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.