Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 time
We can map between the inheritance rational technology and the object oriented technology .In this example will show single table per hierarchy inheritance . In this case we have a single rational database table to represent the entire class hierarchy. There is a discriminator column is used to differ between subclasses.The discriminator column in database will specify the java type which represents.
Advantage of this : It supports polymorphism.
Disadvantage : Table must has column that represents all classes in hierarchy . And each filed maps to a subclass must be null-able .
Here is the JPA class hierarchy :
Code:
package com.codemiles.jpa;
import java.io.Serializable;
import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorType; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; @Entity @Table(name="SHAPE") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="TYPE",discriminatorType=DiscriminatorType.STRING) @DiscriminatorValue("JustShape") public class Shape implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @Id protected int id; protected int scale; protected float rotate; protected String colour; protected String points; protected int numOfPoints; public Shape(){ id=(int)System.currentTimeMillis(); } @Column(name="SHAPE_ID") public int getId(){ return id; }
public void setId(int id){ this.id = id; } @Column(name="SCALE") public int getScale(){ return scale; }
public void setScale(int scale){ this.scale = scale; } @Column(name="ROTATE") public float getRotate(){ return rotate; }
public void setRotate(float rotate){ this.rotate = rotate; } @Column(name="POINTS") public String getPoints(){ return points; }
public void setPoints(String points){ this.points = points; } public void setColour(String colour){ this.colour = colour; }
@Column(name="COLOUR") public String getColour(){ return colour; } public void setNumOfPoints(int numOfPoints){ this.numOfPoints = numOfPoints; } @Column(name="NUM_POINTS") public int getNumOfPoints(){ return numOfPoints; } }
@Entity @DiscriminatorValue("Circle") public class Circle extends Shape implements Serializable{ /** * */ private static final long serialVersionUID = 1L; protected float radius;
public Circle(){ super(); numOfPoints =1 ; }
public void setRadius(float radius){ this.radius = radius; }
@Column(name="RADIUS") public float getRadius(){ return radius; } }
@Entity @DiscriminatorValue("Triangle") public class Triangle extends Shape implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String isosceles ; public Triangle(){ super(); numOfPoints=3; }
public void setIsosceles(String isosceles){ this.isosceles = isosceles; } @Column(name="ISOSCELES") public String getIsosceles(){ return this.isosceles; } @Transient public boolean isIsosceles(){ return isosceles.equals("Y"); }
}
You can see a new annotations in the code snippets : @Inheritance : specify the type of inheritance . @DiscriminatorColumn : used to specify the columns which is used the mapping to java type . @DiscriminatorValue : value of discriminator column for this class.
as you may notice , calling super() function in sub-classes to call the root classes where the simple id is generated.I am using simple id here by @id annotation.
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )