实体映射类:
@Entity
@Table(name="player")
@org.hibernate.annotations.Entity(dynamicUpdate=true,dynamicInsert=true)//加上这句插入实体映射类时空字段才能插入默认的值
public class Player {
@Id
@Column(name="id",nullable=false)
private String id;
@Column(name="name",nullable=false)
private String name;
@Column(name="level",nullable=false,columnDefinition="smallint default 1")//定义当此属性为空时,插入的默认值,要不为空状态下
private Short level;
@Column(name="sex",nullable=false,columnDefinition="tinyint default 0")
private Byte sex;
@Column(name="created",nullable=true,columnDefinition="datetime default null")//默认为空时
private Date created;
………………
set,get省略了………
dao层下:
sessionFactory.getCurrentSession().save(player);
player对象里面的属性此时有些是空的,插入到数据库后,数据库的空字段数据是默认值