• 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 + "]";
        }
    }
    
    

  • 相关阅读:
    axis2的wsdl无法使用eclipse axis1插件来生成client--解决方法
    引用的存在价值
    阿里亲心小号实測
    UVA 1328
    XMPP 协议工作流程具体解释
    10g异机恢复后EM无法启动故障处理一例
    JVM 内存
    abstract class和interface有什么区别?
    ArrayList 如何增加大小
    IndexOutOfBoundsException ArrayList 访问越界
  • 原文地址:https://www.cnblogs.com/xiaoqiangzhaitai/p/5429561.html
Copyright © 2020-2023  润新知