I'm working on a Spring(4.x)-Hibernate(5.2.x) web-application project.
Model class
@Entity
@Table( name = "users" )
public class User {
@Id
@Column( name="ID" )
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
@Temporal( TemporalType.TIMESTAMP )
@Column( name="CREATED_AT", insertable=false, nullable=false )
@Generated( GenerationTime.INSERT )
private Date createdAt;
@Temporal( TemporalType.TIMESTAMP )
@Column( name="UPDATED_AT", insertable=false, updatable=false, nullable=false )
@Generated( GenerationTime.ALWAYS )
private Date updatedAt;
public enum Role {
ADMIN,
USER,
GUEST
}
@NotNull
@Column( name="ROLE" )
@Enumerated( EnumType.STRING )
private Role role;
//getters & setters
}
Hibernate properties
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update
Auto generated query
create table users (
ID bigint not null auto_increment,
CREATED_AT datetime not null,
UPDATED_AT datetime not null,
ROLE varchar(255) not null,
primary key (id)
)
I expect a query which has
CREATED_AT DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMPandUPDATED_AT DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMPandROLE ENUM('ADMIN','USER','GUEST') VARCHAR(255) NOT NULL
Is this the way it works or is it my mistake in configuration? Done some search, but no clear solution found, except these,
@Column(name="CREATED_AT", nullable = false,columnDefinition="TIMESTAMP default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP")@ColumnDefault( "NOW()" )
But these definitions depends on the underlying database, that's what I think. Need a configuration which is more java/hibernate wise, like I used, but not working!