Closed RammusXu closed 7 years ago
使用 timestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(updatable = false,
insertable = false,
columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP")
public Date create_at = Calendar.getInstance().getTime();
@Column(updatable = false,
insertable = false,
columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
public Date update_at = Calendar.getInstance().getTime();
使用 compound key (index / unique)
@Entity
@Table(
uniqueConstraints = {@UniqueConstraint(columnNames = {"brand_id", "account"})}
)
@Index(columnNames = {"status", "brand_id"})
public class User extends Model {
使用 compound primary key
package models.pk;
import javax.persistence.Embeddable;
import java.io.Serializable;
@Embeddable
public class MyPK implements Serializable {
public String name;
public String email;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyPK embId = (AppVersionPK) o;
if (!name.equals(embId.name)) return false;
if (!email.equals(embId.email)) return false;
return true;
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + email.hashCode();
return result;
}
}
使用 MyPK
@Entity
public class MyModel extends Model {
@EmbeddedId
public MyPK id;
}
使用 Enum
public class User extends Model {
public enum UserStatus {
NEW,
DELETED,
VERIFIED
}
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "enum('NEW','DELETED','VERIFIED') default 'NEW'")
public UserStatus status = UserStatus.NEW;
}
使用 sql query
String sql = "select COUNT(*) as count from schema_version";
Long count = Ebean.createSqlQuery(sql).findUnique().getLong("count");
觀念
小技巧
\G
可以將表格轉成直式顯示常用指令
Foriegn Key
Primary Key
CRUD
Charset