EJB3.0 @Column设置precision和scale转换ORACLE中的Number(x,y) 博客分类: EJB3.0oracle EJBORACLE
程序员文章站
2024-03-17 08:35:52
...
今天看了下Hibernate的@Column注解
@Column(
name="columnName"; 1
boolean unique() default false; 2
boolean nullable() default true; 3
boolean insertable() default true; 4
boolean updatable() default true; 5
String columnDefinition() default ""; 6
String table() default ""; 7
int length() default 255; 8
int precision() default 0; 9
int scale() default 0; 10
)
除了7、9、10,其他都容易理解。
其中9和10是设置decimal的精度,开始定义了Float和Double类型的字段,然后在这两种类型的字段上使用@Column注解,结果9和10不起作用。最后发现在使用BigDecimal类型时,9和10才起作用。如果自己使用columnDefinition定义类型(@Column(columnDefinition="decimal")),则需要自己定义精度,9和10是不起作用的,同理,使用Double和Float类型时MySQL中生成的是double和float,于是也不会起作用。而7要和@SecondaryTable注解一起使用。
@Entity @Table(name = "BSCCPUBURDEN") public class BSCCPUBurden implements Serializable { private static final long serialVersionUID = 810923802091774744L; private String id; /** * 起始时间 */ private Date startDate; /** * 周期 */ private String period; /** * 对象名称 */ private String objName; /** * CPU占用率平均值 */ private BigDecimal cpuOccupancyAvg; /** * CPU占用率最大值 */ private BigDecimal cpuOccupancyMax; @Id @Column(name = "ID_") public String getId() { return id; } public void setId(String id) { this.id = id; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "STARTDATE_") public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } @Column(name = "PERIOD_") public String getPeriod() { return period; } public void setPeriod(String period) { this.period = period; } @Column(name = "OBJNAME_") public String getObjName() { return objName; } public void setObjName(String objName) { this.objName = objName; } @Column(name = "CPUOCCUPANCYAVG_", precision = 10 , scale = 3) public BigDecimal getCpuOccupancyAvg() { return cpuOccupancyAvg; } public void setCpuOccupancyAvg(BigDecimal cpuOccupancyAvg) { this.cpuOccupancyAvg = cpuOccupancyAvg; } @Column(name = "CPUOCCUPANCYMAX_", precision = 10, scale = 3) public BigDecimal getCpuOccupancyMax() { return cpuOccupancyMax; } public void setCpuOccupancyMax(BigDecimal cpuOccupancyMax) { this.cpuOccupancyMax = cpuOccupancyMax; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; BSCCPUBurden other = (BSCCPUBurden) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } }