Powered By Blogger

Sunday, December 4, 2011

JSON Notes

JSON is abbreviation of Javascript Object and Array Notation
=======================
create an array in Javascript:
var myArray = new Array("Taipei", "Taichung", "Tainan");
or
var myArray = ["Taipei", "Taichung", "Tainan"];
Javascript array can be a mixed array which contains any type of object, string , number or customized object.
create an object in Javascript:
var myDog = new Object( );
myDog.name = "Allen";
myDog.age = 5;
myDog.color = "blue";
=======================
or
var myCat = {
   "name":"Allen";
   "age":5;
   "color":"blue";
}
=======================
a mixed use of array and object
var myDogs = [ {
   "name":"Allen";
   "age":5;
   "color":"blue"; },{
   "name":"Ben";
   "age":6;
   "color":"red";} ];
alert(myDogs[0].name);
alert(myDogs[1].age);
=======================
Example of JSON
var json = '{"myCats": [ {"name": "Hero", "age": 5, "color": "silver" }, {"name": "Euro", "age": 2, "color": ["brown", "white", "black"] }]}';
var obj = eval ("(" + json + ")"); 
alert('I have ' + obj.myCats.length + ' cats.'); 
alert(obj.myCats[0].name); 
alert(obj.myCats[1].name);
But eval( ) is dangerous for XSS attack so we use parser instead.
var obj = JSON.parse(json); 
alert('I have ' + obj.myCats.length + ' cats.'); 
alert(obj.myCats[0].name);
alert(obj.myCats[1].name);
=======================
Here is the way transfer an object to JSON string.
<script type="text/javascript" src="http://www.json.org/json.js"></script> 
<script type="text/javascript">
 var obj = new Object(); 
obj.myCats = new Array(new Object(), new Object()); 
obj.myCats[0].name = "Hero"; 
obj.myCats[0].age = 5; 
obj.myCats[0].color = "silver"; 
obj.myCats[1].name = "Euro"; 
obj.myCats[1].age = 2; 
obj.myCats[1].color = ["black", "white", "brown"]; 
alert(JSON.stringify (obj)); 
</script>

























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;
} 
}