วันอังคารที่ 10 ธันวาคม พ.ศ. 2556

Annotation Type Hibernate Java TableGenerator

 /*สร้าง Entity ใน Hibernate แล้วทำการให้ Gen Id ให้อัตโนมัติ ด้วย Anotation Type*/
/*Example*/
package cdgs.account.domain;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.servlet.http.HttpServletRequest;

import aczept.utils.AuditContextLocal;
import cdgs.account.utils.AccountUtils;

@Entity
@Table(name="SH_MEMBER")
@TableGenerator(
    name="counter",
    table="AC_COUNTER",
    pkColumnName="name",
    valueColumnName="value",
    pkColumnValue="SH_MEMBER",
    initialValue=1,
    allocationSize=1)
public class Member implements Serializable {
    private static final long serialVersionUID = -6419644899976540495L;
    private Integer memberId;
    private Integer departmentId;
    private Integer personId;
    private String memberNo;
    private Integer memberTypeId;
    private Integer memberRequestId;
    private Integer memberRequestRelationId;
    private Date registerDate;
    private String memberSource;
    private Integer donatorId;
    private String donatorRelate;
    private String isCancel;
    private Date cancelDate;
    private Integer cancelPerId;
    private String isLockUpdate;
    private Date createTime;
    private String createUserId;
    private String createProg;
    private Date lastUpdTime;
    private String lastUpdUserId;
    private String lastUpdProg;
    private int lastUpdVersion;
    private Integer businessUnitId;
    private Date oldRegisterDate;
    private BigDecimal oldTotalAmount;
    private BigDecimal oldPaidAmount;
    private Date oldPaidDate;
    private Integer oldPaidYear;
    private Integer oldPaidTerm;
    private String oldMemberNo;
    private BigDecimal amount;
    private String addition;
       
    @Id
    @GeneratedValue(strategy=GenerationType.TABLE, generator="counter")
    @Column(name="MEMBER_ID")
    public Integer getMemberId() {
        return this.memberId;
    }

    public void setMemberId(Integer memberId) {
        this.memberId = memberId;
    }

    @Column(name="PERSON_ID")
    public Integer getPersonId() {
        return this.personId;
    }

    public void setPersonId(Integer personId) {
        this.personId = personId;
    }

    @Column(name="MEMBER_NO")
    public String getMemberNo() {
        return this.memberNo;
    }

    public void setMemberNo(String memberNo) {
        this.memberNo = memberNo;
    }

    @Column(name="MEMBER_TYPE_ID")
    public Integer getMemberTypeId() {
        return this.memberTypeId;
    }

    public void setMemberTypeId(Integer memberTypeId) {
        this.memberTypeId = memberTypeId;
    }

    @Column(name="MEMBER_REQUEST_ID")
    public Integer getMemberRequestId() {
        return this.memberRequestId;
    }

    public void setMemberRequestId(Integer memberRequestId) {
        this.memberRequestId = memberRequestId;
    }

    @Column(name="MEMBER_SOURCE")
    public String getMemberSource() {
        return this.memberSource;
    }

    public void setMemberSource(String memberSource) {
        this.memberSource = memberSource;
    }

    @Column(name="DONATOR_ID")
    public Integer getDonatorId() {
        return this.donatorId;
    }

    public void setDonatorId(Integer donatorId) {
        this.donatorId = donatorId;
    }

    @Column(name="DONATOR_RELATE")
    public String getDonatorRelate() {
        return this.donatorRelate;
    }

    public void setDonatorRelate(String donatorRelate) {
        this.donatorRelate = donatorRelate;
    }

    @Column(name="IS_CANCEL")
    public String getIsCancel() {
        return this.isCancel;
    }

    public void setIsCancel(String isCancel) {
        this.isCancel = isCancel;
    }

    @Column(name="CANCEL_DATE")
    public Date getCancelDate() {
        return this.cancelDate;
    }

    public void setCancelDate(Date cancelDate) {
        this.cancelDate = cancelDate;
    }

    @Column(name="CANCEL_PER_ID")
    public Integer getCancelPerId() {
        return this.cancelPerId;
    }

    public void setCancelPerId(Integer cancelPerId) {
        this.cancelPerId = cancelPerId;
    }

    @Column(name="IS_LOCK_UPDATE")
    public String getIsLockUpdate() {
        return this.isLockUpdate;
    }

    public void setIsLockUpdate(String isLockUpdate) {
        this.isLockUpdate = isLockUpdate;
    }

    @Column(name="CREATE_TIME")
    public Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Column(name="CREATE_USER_ID")
    public String getCreateUserId() {
        return this.createUserId;
    }

    public void setCreateUserId(String createUserId) {
        this.createUserId = createUserId;
    }

    @Column(name="CREATE_PROG")
    public String getCreateProg() {
        return this.createProg;
    }

    public void setCreateProg(String createProg) {
        this.createProg = createProg;
    }

    @Column(name="LAST_UPD_TIME")
    public Date getLastUpdTime() {
        return this.lastUpdTime;
    }

    public void setLastUpdTime(Date lastUpdTime) {
        this.lastUpdTime = lastUpdTime;
    }

    @Column(name="LAST_UPD_USER_ID")
    public String getLastUpdUserId() {
        return this.lastUpdUserId;
    }

    public void setLastUpdUserId(String lastUpdUserId) {
        this.lastUpdUserId = lastUpdUserId;
    }

    @Column(name="LAST_UPD_PROG")
    public String getLastUpdProg() {
        return this.lastUpdProg;
    }

    public void setLastUpdProg(String lastUpdProg) {
        this.lastUpdProg = lastUpdProg;
    }

    @Version
    @Column(name="LAST_UPD_VERSION")
    public int getLastUpdVersion() {
        return this.lastUpdVersion;
    }

    public void setLastUpdVersion(int lastUpdVersion) {
        this.lastUpdVersion = lastUpdVersion;
    }

    @Column(name="BUSINESS_UNIT_ID")
    public Integer getBusinessUnitId() {
        return this.businessUnitId;
    }

    public void setBusinessUnitId(Integer businessUnitId) {
        this.businessUnitId = businessUnitId;
    }

    @Column(name="REGISTER_DATE")
    public Date getRegisterDate() {
        return registerDate;
    }

    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }

    @Column(name="MEMBER_REQUEST_RELATION_ID")
    public Integer getMemberRequestRelationId() {
        return memberRequestRelationId;
    }

    public void setMemberRequestRelationId(Integer memberRequestRelationId) {
        this.memberRequestRelationId = memberRequestRelationId;
    }

    @Column(name="DEPARTMENT_ID")
    public Integer getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Integer departmentId) {
        this.departmentId = departmentId;
    }

    @Column(name="OLD_REGISTER_DATE")
    public Date getOldRegisterDate() {
        return oldRegisterDate;
    }

    public void setOldRegisterDate(Date oldRegisterDate) {
        this.oldRegisterDate = oldRegisterDate;
    }

    @Column(name="OLD_TOTAL_AMOUNT")
    public BigDecimal getOldTotalAmount() {
        return oldTotalAmount;
    }

    public void setOldTotalAmount(BigDecimal oldTotalAmount) {
        this.oldTotalAmount = oldTotalAmount;
    }

    @Column(name="OLD_PAID_AMOUNT")
    public BigDecimal getOldPaidAmount() {
        return oldPaidAmount;
    }

    public void setOldPaidAmount(BigDecimal oldPaidAmount) {
        this.oldPaidAmount = oldPaidAmount;
    }

    @Column(name="OLD_PAID_DATE")
    public Date getOldPaidDate() {
        return oldPaidDate;
    }

    public void setOldPaidDate(Date oldPaidDate) {
        this.oldPaidDate = oldPaidDate;
    }

    @Column(name="OLD_PAID_YEAR")
    public Integer getOldPaidYear() {
        return oldPaidYear;
    }

    public void setOldPaidYear(Integer oldPaidYear) {
        this.oldPaidYear = oldPaidYear;
    }

    @Column(name="OLD_PAID_TERM")
    public Integer getOldPaidTerm() {
        return oldPaidTerm;
    }

    public void setOldPaidTerm(Integer oldPaidTerm) {
        this.oldPaidTerm = oldPaidTerm;
    }

    @Column(name="OLD_MEMBER_NO")
    public String getOldMemberNo() {
        return oldMemberNo;
    }

    public void setOldMemberNo(String oldMemberNo) {
        this.oldMemberNo = oldMemberNo;
    }

    @Column(name="AMOUNT")
    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }
   
    @Column(name="ADDITION")
    public String getAddition() {
        return addition;
    }

    public void setAddition(String addition) {
        this.addition = addition;
    }

    @Transient
    public MemberType getMemberType() {
        if(memberTypeId == null) return null;
       
        HttpServletRequest request = AuditContextLocal.getContext().getRequest();
        AccountDB adb = AccountUtils.getAccountDB(request);
        return adb.getMemberTypeById(this.memberTypeId);
    }
}

/*รายละเอียดการใช้งานต่างๆ*/
http://docs.oracle.com/javaee/6/api/javax/persistence/TableGenerator.html

ไม่มีความคิดเห็น:

แสดงความคิดเห็น