Powered By Blogger

Thursday, December 1, 2011

Hibernate @OneToOne

============================================================================
@Entity
@Table(name="Master")
public class Master implements Serializable{
       private MasterId id;
       private String Sex; 


       private Detail detail;


       @EmbeddedId
       @AttributeOverrides({
             @AttributeOverride(name="pId", column= @Column( name="P_ID", nullable=false)),
             @AttributeOverride(name="pName", column= @Column( name="P_NAME", nullable=false)) })
       public MasterId getId( ){
           return this.id;
       }
       public void setId(MasterId id) {
           this.id=id;
       }


       @Column(name="SEX", nullable=false, length=4)
       public String getSex( ){
           return this.Sex;
       }
       public void setSex(String Sex){
           this.Sex=Sex;
       }


       @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
       @JoinColumn(name="P_ID", insertable=false , updatable=false)
       public Detail getDetail( ){
            return detail;
       }
       public void setDetail(Detail detail){
            this.detail=detail;
       }
}
============================================================================


@Embeddable
public MasterId implements Serializable{


        private String pId;
        private String pName;


        @Column(name = "P_ID", nullable = false, length=15)
public String getPId( ) {
return this.pId;
}
public void setPId(String pId) {
this.pId = pId;
}


        @Column(name = "P_NAME", nullable = false, length=15)
public String getPName( ) {
return this.pName;
}
public void setPName(String pName) {
this.pName = pName;
}


//public boolean equals(Object other) { }


//public int hashCode( ) { }
}
============================================================================
@Entity
@Table(name = "DETAIL")
public class Detail implements Serializable{
       private String pId;


       @Id
       @Column(name = "P_ID", unique = true, nullable=false)
       public String getPId( ) {
return this.pId;
}
public void setPId(String pId) {
this.pId = pId;
} 
}

No comments:

Post a Comment