hibernate 创建数据中一张表的方法

来源:互联网 发布:卸载mac上的软件 编辑:程序博客网 时间:2024/06/10 02:06
package com.jeefw.model.sys;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;


import com.google.common.base.Objects;
import com.jeefw.model.sys.param.DictParameter;


/**
 * StockAccount  的实体类
 */
@Entity
@Table(name = "stockaccount")
@Cache(region = "all", usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIgnoreProperties(value = { "maxResults", "firstResult", "topCount", "sortColumns", "cmd", "queryDynamicConditions", "sortedConditions", "dynamicProperties", "success", "message", "sortColumnsString", "flag" })
public class StockAccount extends DictParameter {


// 各个字段的含义请查阅文档的数据库结构部分
private static final long serialVersionUID = -2847988368314689488L;
@Id
@GeneratedValue
@Column(name = "stockAccount_id")
private Long id;
@Column(name = "account", length = 255, nullable = false, unique = true)
private String Account;
@Column(name = "Persion_name", length = 255, nullable = false)
private String persionName;


public StockAccount() {


}


public Long getId() {
return id;
}


public void setId(Long id) {
this.id = id;
}



public String getAccount() {
return Account;
}


public void setAccount(String account) {
Account = account;
}


public String getPersionName() {
return persionName;
}


public void setPersionName(String persionName) {
this.persionName = persionName;
}


public static long getSerialversionuid() {
return serialVersionUID;
}


public boolean equals(Object obj) {
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final StockAccount other = (StockAccount) obj;
return Objects.equal(this.id, other.id) && Objects.equal(this.persionName, other.persionName) 
&& Objects.equal(this.Account, other.Account);
}

public int hashCode() {
return Objects.hashCode(this.id, this.Account, this.persionName);
}

}

在配置好hibernate之后,我们需要添加实体类,并且在数据库中生成对应的表。

上面这个类就是一个实体类,并且使用注解的方式配置出了关系映射模型。因此,也就不需要添加诸如.hbm.xml之类的关系映射文件。

在这个实体类中,我们定义了id,及其他的属性。

然后运行程序,就能在数据库中找到对应的表。

关于date的问题,解决办法如下:

定义date类型的变量

@Column(name = "refresh_time")
@Temporal(TemporalType.TIMESTAMP)

private Date refreshTime;


下面是在date类型的变量中,get方法前添加一个标注


@JsonSerialize(using = DateTimeSerializer.class)

public Date getBuyDate() {
return buyDate;
}

这样就定义了一个在数据库和程序中都能够使用的时间类型。



0 0