hibernate注解之外键关联表(@OneToOne)

笔者在使用hibernate注解时需要建一个表关联

通过本次建表收获了一些意外的心得

1、在使用@OneToOne注解时数据库相应表中不必有外键
2、注解必须写在一致的位置上,即要么全写在实体的字段上,要么全部写在get方法上

3、当要做冗余时;即在数据字段中要多建一个所参考的外键表的主键字段需要设置其中一个字段为不可写和不可更新

一般console报错误为:Repeated column in mapping for entity: net.shopxx.qzhprp.entity.GpPpDeployAccessories column: component_id (should be mapped with insert="false" update="false")

解决办法为:添加注解如,  @JoinColumn(name = "componentId",referencedColumnName="SEQ_ID",insertable=false,updatable=false)

具体参考一下例子(example):


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 *
 * 中文名:    选购配件
 * 表名:      [shopxx3 (shopxx3)].[GP_PP_DEPLOY_ACCESSORIES]
 * @author   Zhang Xiaopeng
 * @version  1.0
 * @date     2013/10/21 21:01:01 
 */
@Entity
@Table(name="GP_PP_DEPLOY_ACCESSORIES")
public class GpPpDeployAccessories {
    // ------------------------------ FIELDS ------------------------------
    /** seq_id(NUMBER(36)) */
    private String seqId;

    /** (NUMBER(36)) */
    private String productId;   

    /** (VARCHAR2(100)) */
    private String componentInformation;

    /** (NUMBER(8,2)) */
    private Double price;

    /** (NUMBER(36)) */
    private String accesoryId;

    /** (char(36)) */
    private String componentId;
   
    /** (DATE) */
    private Date createDate;

    /** (DATE) */
    private Date modifyDate;
   
    /** 是否标配 */
    private String isStandard;
   
    /** 购物车键 */
    private String cartKey;
   
    /** 组件 */  
    private GpPpComponent gpPpComponent;
   
    /** 配件 */
    private GpPpAccessory GpPpAccessory;
   
    // --------------------- GETTER / SETTER METHODS ---------------------
    /**
    * @return seq_id(NUMBER(36))
    */
    @Id 
    @Column(name = "SEQ_ID", nullable = false)  
    public String getSeqId() {
        return seqId;
    }

    /**
    * @param seqId seq_id(NUMBER(36))
    */
    public void setSeqId(String seqId) {
        this.seqId = seqId;
    }

    /**
    * @return (NUMBER(36))
    */
    public String getProductId() {
        return productId;
    }

    /**
    * @param productId (NUMBER(36))
    */
    public void setProductId(String productId) {
        this.productId = productId;
    }

    /**
    * @return (VARCHAR2(100))
    */
    public String getComponentId() {
        return componentId;
    }

    /**
    * @param componentId (VARCHAR2(100))
    */
    public void setComponentId(String componentId) {
        this.componentId = componentId;
    }

    /**
    * @return (VARCHAR2(100))
    */
    public String getComponentInformation() {
        return componentInformation;
    }

    /**
    * @param componentInformation (VARCHAR2(100))
    */
    public void setComponentInformation(String componentInformation) {
        this.componentInformation = componentInformation;
    }

    /**
    * @return (NUMBER(8,2))
    */
    public Double getPrice() {
        return price;
    }

    /**
    * @param price (NUMBER(8,2))
    */
    public void setPrice(Double price) {
        this.price = price;
    }

    /**
    * @return (NUMBER(36))
    */
    public String getAccesoryId() {
        return accesoryId;
    }

    /**
    * @param accesoryId (NUMBER(36))
    */
    public void setAccesoryId(String accesoryId) {
        this.accesoryId = accesoryId;
    }

    /**
    * @return (DATE)
    */
    public Date getCreateDate() {
        return createDate;
    }

    /**
    * @param createDate (DATE)
    */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public void setCreateDate(String createDate) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            this.createDate = df.parse(createDate);
        } catch (Exception e) {
            System.err.println(e.getMessage()+ e.getStackTrace());
        }
    }

    public String getIsStandard() {
        return isStandard;
    }

    public void setIsStandard(String isStandard) {
        this.isStandard = isStandard;
    }

    public String getCartKey() {
        return cartKey;
    }

    public void setCartKey(String cartKey) {
        this.cartKey = cartKey;
    }

    /**
    * @return (DATE)
    */
    public Date getModifyDate() {
        return modifyDate;
    }

    /**
    * @param modifyDate (DATE)
    */
    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    public void setModifyDate(String modifyDate) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            this.modifyDate = df.parse(modifyDate);
        } catch (Exception e) {
            System.err.println(e.getMessage()+ e.getStackTrace());
        }
    }
    @OneToOne(cascade=CascadeType.ALL) 
    @JoinColumn(name = "componentId",referencedColumnName="SEQ_ID",insertable=false,updatable=false)
  
    public GpPpComponent getGpPpComponent() {
        return gpPpComponent;
    }

    public void setGpPpComponent(GpPpComponent gpPpComponent) {
        this.gpPpComponent = gpPpComponent;
    }
    @OneToOne(cascade=CascadeType.ALL) 
    @JoinColumn(name = "accesoryId",referencedColumnName="SEQ_ID",insertable=false,updatable=false)
    public GpPpAccessory getGpPpAccessory() {
        return GpPpAccessory;
    }

    public void setGpPpAccessory(GpPpAccessory gpPpAccessory) {
        GpPpAccessory = gpPpAccessory;
    }

}

 

你可能感兴趣的:(Hibernate,jpa,ontoone)