Total members 11893 |It is currently Thu Nov 14, 2024 6:55 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





In this case there is a table for each subclass in the class model .The table for subclass only contains the properties that are defined in the subclasses . To do this you will need changes in code from the single table inheritance example. There is no discriminator in the subclasses . In the super class you will need to change the inheritance type to JOINED . This technique still support polymorphism.

Code:
@Inheritance(strategy=InheritanceType.JOINED)
 


JOINED , means there must be a join relation between the tables . The id of the super class is used as Foreign key in the database tables of the subclasses in the JPA class model .

Another thing , @Table annotation is added to subclasses entities , because it is now related to a tables .
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.JOINED)
@
DiscriminatorColumn(name="TYPE",discriminatorType=DiscriminatorType.STRING)
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;
    }
    
}
  


Code:

package com
.codemiles.jpa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@
Entity
@Table(name="CIRCLE")
public class Circle extends Shape implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected float radius; 
  

    
    public Circle
(){
    super();
    numOfPoints =;
    
    
}


    public void setRadius(float radius) {
    this.radius = radius;
    }

    @Column(name="RADIUS")
    public float getRadius() {
    return radius;
    }
}

  


Code:

package com
.codemiles.jpa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Transient;

@
Entity
@Table(name="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");
    }

 
}
  



A note we have to mention about EJB-QL , The Join must be done in order to retrieve the subclasses properties . For example like this :
Code:

SELECT s
.colour , s.points , c.radius FROM Shape s , Circle c WHERE s.id=c.id;
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : separate table per subclass
 Is a class a subclass of itself     -  
 separate border collapse example     -  
 Using table in latex     -  
 table creation     -  
 row highlight of table     -  
 table row hover     -  
 Creating table using div tag     -  
 Div Table with css row span     -  
 Shorthand Operators table     -  
 secondary table per entity     -  



Topic Tags

Java JPA






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com