• EJB>多表映射 小强斋


    实体的成员属性映射的字段分布在多张表中,如何处理?
    本例子的实体MainTable具有四个成员属性:idnameaddresspostcode
    其中与idname映射的字段在MainTable表,与addresspostcode属性映射的字段在Address表。
    需要使用@javax.persistence.SecondaryTable注释和@Column.table()属性
    import java.io.Serializable;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.PrimaryKeyJoinColumn;
    import javax.persistence.SecondaryTable;
    
    
    @Entity
    // name 另一张表的表名 pkJoinColumns 另一张表的PK
    @SecondaryTable(name = "Address", 
    		pkJoinColumns = {@PrimaryKeyJoinColumn(name = "address_id")} )
    		
    public class MainTable implements Serializable{
    	private static final long serialVersionUID = 4016777473296175005L;
    	private Integer id;// 主键
    	private String name;// 姓名
    	private String address;// 地址,该映射的字段分布在Address表
    	private String postcode;// 邮编,该映射的字段分布在Address表
    
        @Id 
        @GeneratedValue
    	public Integer getId() {
    		return id;
    	}
    
    	public void setId(Integer id) {
    		this.id = id;
    	}
    	
    	@Column(nullable=false,length=32)
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	// 指定映射到表名为 Address 的表中
    	@Column(table="Address")
    	public String getAddress() {
    		return address;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    	
    	// 指定映射到表名为 Address 的表中
    	@Column(length=6, table="Address")
    	public String getPostcode() {
    		return postcode;
    	}
    
    	public void setPostcode(String postcode) {
    		this.postcode = postcode;
    	}
    
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (this.id != null ? this.id.hashCode() : 0);
            return hash;
        }
        
        @Override
        public boolean equals(Object object) {
            if (!(object instanceof MainTable)) {
                return false;
            }
            MainTable other = (MainTable)object;
            if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
            return true;
        }
    
        @Override
        public String toString() {
            return this.getClass().getName()+ "[id="+id+ ",name="+ name+ ",address="+ address+ ",postcode="+ postcode + "]";
        }
    }
    
    

  • 相关阅读:
    HeapSpray初窥(2014.12)
    CVE-2014-4115漏洞分析(2014.11)
    【原创】oracle提权执行命令工具oracleShell v0.1
    【原创】贴个dirtycow(脏牛漏洞)不死机的exploit
    【CVE-2016-10009】OpenSSH < 7.4
    关于elasticsearch和kibana的时区和日期问题
    这是我写过的最长的正则表达式,没有之一
    三生缘
    【原创】JEECMS v6~v7任意文件上传漏洞(2)
    【原创】JEECMS v6~v7任意文件上传漏洞(1)
  • 原文地址:https://www.cnblogs.com/xiaoqiangzhaitai/p/5637638.html
Copyright © 2020-2023  润新知