`
ranyut
  • 浏览: 255951 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

EJB3实体映射之OneToOne

    博客分类:
  • EJB3
阅读更多
Person  类:
import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Person  implements Serializable{
	private static final long serialVersionUID = 8305710085432450318L;
	private Integer personid;
    private String name;    
    private boolean sex;
    private Short age;
    private Date birthday;
    private IDCard idcard;
  
    @Id
    @GeneratedValue   
    public Integer getPersonid() {
        return personid;
    }
    public void setPersonid(Integer personid) {
        this.personid = personid;
    } 
    
    @Column(name = "PersonName",nullable=false,length=32)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Column(nullable=false)
    public boolean getSex() {
        return sex;
    }
    public void setSex(boolean sex) {
        this.sex = sex;
    }
    
    @Column(nullable=false)   
    public Short getAge() {
        return age;
    }
    public void setAge(Short age) {
        this.age = age;
    } 
    
    @Temporal(value=TemporalType.DATE)
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
 
    @OneToOne(optional = true, cascade = CascadeType.ALL)  
    @JoinColumn(name = "idcard_id", unique = true)  
    // person是主体 person中保存idcard的外键
    public IDCard getIdcard() {
        return idcard;   
    }   
    public void setIdcard(IDCard idcard) {  
        this.idcard = idcard;   
    } 
    /**
     * 返回对象的散列代码值。该实现根据此对象
     * 中 personid 字段计算散列代码值。
     * @return 此对象的散列代码值。
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.personid != null ? this.personid.hashCode() : 0);
        return hash;
    }

    /**
     * 确定其他对象是否等于此 Person。当且仅当
     * 参数不为 null 且该参数是具有与此对象相同 personid 字段值的 Person 对象时,
     * 结果才为 <code>true</code>。
     * @param 对象,要比较的引用对象
     * 如果此对象与参数相同,则 @return <code>true</code>;
     * 否则为 <code>false</code>。
     */
    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Person)) {
            return false;
        }
        Person other = (Person)object;
        if (this.personid != other.personid && (this.personid == null || !this.personid.equals(other.personid))) return false;
        return true;
    }

    /**
     * 返回对象的字符串表示法。该实现根据 personid 字段
     * 构造此表示法。
     * @return 对象的字符串表示法。
     */
    @Override
    public String toString() {
        return this.getClass().getName()+ "[personid=" + personid+ "]";
    }
}



IDCard  类:
import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class IDCard  implements Serializable{
	private static final long serialVersionUID = -4610000117411667607L;
	private Integer id;
    private String cardno;    
    private Person person;
  
    public IDCard() {}
    
    public IDCard(String cardno) {
        this.cardno = cardno;
    }
    
    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Column(nullable=false,length=18,unique = true)
    public String getCardno() {
        return cardno;
    }
    public void setCardno(String cardno) {
        this.cardno = cardno;
    }
    
    @OneToOne(optional = false, cascade = CascadeType.REFRESH, mappedBy = "idcard")
    // 对应person表中的idcard属性
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
    /**
     * 返回对象的散列代码值。该实现根据此对象
     * 中 id 字段计算散列代码值。
     * @return 此对象的散列代码值。
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    /**
     * 确定其他对象是否等于此 IDCard。当且仅当
     * 参数不为 null 且该参数是具有与此对象相同 id 字段值的 IDCard 对象时,
     * 结果才为 <code>true</code>。
     * @param 对象,要比较的引用对象
     * 如果此对象与参数相同,则 @return <code>true</code>;
     * 否则为 <code>false</code>。
     */
    @Override
    public boolean equals(Object object) {
        if (!(object instanceof IDCard)) {
            return false;
        }
        IDCard other = (IDCard)object;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
        return true;
    }

    /**
     * 返回对象的字符串表示法。该实现根据 id 字段
     * 构造此表示法。
     * @return 对象的字符串表示法。
     */
    @Override
    public String toString() {
        return this.getClass().getName()+ "[id=" + id + "]";
    } 
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics