Posted at

【メモ】Hibernateでinsert時にprimary keyでauto incrementを指定している時

More than 3 years have passed since last update.


概要

ERROR [2015-02-27 11:01:38,781] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: ef46ad41b77903a3

! com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hoge.hibernate_sequence' doesn't exist
! at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_20]

こんな感じのエラーが出たので対応したい


コード


hoge.bean.hoge.java

package hoge.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.joda.time.DateTime;

@Entity
@Table(name="hoge_table")
public class HogeTable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY) // これをつけて解決
@Column(name="id")
private Integer id;
@Column(name="created")
private DateTime created;
@Column(name="modified")
private DateTime modified;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}

public DateTime getCreated() {
return created;
}
public void setCreated(DateTime created) {
this.created = created;
}
public DateTime getModified() {
return modified;
}
public void setModified(DateTime modified) {
this.modified = modified;
}
}


元ネタのstackoverflow