联合主键
@IdClass来设置多个主键
1、先写一个包含主键的类
class PrimaryKey implements Serializable {
Integer id;
Integer userId;
}
2、在Entity类中,按照如下方式使用
@Entity
@Table(indexes = [ // 定义数据库索引。
// 唯一索引。
@Index(name = "ux_user_login_name", columnList = "loginName", unique = true),
// 非唯一索引。
@Index(name = "idx_user_age", columnList = "age")
])
@IdClass(PrimaryKey.class)
class MyEntity {
/**
* @description 主键
*/
@Id
@Column(name = "id", nullable = false)
Integer id
/**
* @description 主键
*/
@Id
@Column(name = "user_id", nullable = false)
Integer userId
/**
* 用户的登录名。
*/
@Column(length = 100, nullable = false)
String loginName
/**
* 用户的年龄。
*/
@Column(length = 3)
Integer age
/**
* 用户的状态。
*/
// @Column(length = 16, nullable = false)
// @Enumerated(EnumType.STRING)
// UserStatus status = UserStatus.enable;
/**
* 用户的注册时间。
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Date registTime;
}