I will try to understand you with an example. Suppose you had a relational table (STUDENT) with two columns and an identifier (int) and NAME (String). Now, as an ORM, you would make an entity class somewhat as follows: -
package com.kashyap.default; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "STUDENT") public class Student implements Serializable { private static final long serialVersionUID = -1354919370115428781L; @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "NAME") private String name; public Student(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Suppose the table already has entries. Now, if someone asks to add another column "AGE" (int)
ALTER TABLE STUDENT ADD AGE int NULL
You will need to set the default values ββto NULL to add another column to the pre-populated table. This forces you to add another field to the class. Now the question is whether you will use a primitive data type or a non-primitive wrapper data type to declare a field.
@Column(name = "AGE") private int age;
or
@Column(name = "AGE") private INTEGER age;
you need to declare the field as a non-primitive wrapper data type, because the container will try to map the table to the entity. Therefore, it will not be able to display NULL values ββ(by default) if you do not declare the field as a wrapper and eventually throw away "Null value was assigned to the primitive type property" Exception ".
codechefvaibhavkashyap Jun 11 '16 at 17:53 on 2016-06-11 17:53
source share