Total members 11889 |It is currently Fri Mar 29, 2024 9:20 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Following example show you how to use @AttributeOverrides within your entities when you have @Embeddable entity. In the is example Transformation entity is an @Embeddable.Embeddable class is a part of entity .Fields of embeddable class are mapped to the table of the owning entity.

Follow the example below :
Code:

package com
.codemiles.jpa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embedded;
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.TABLE_PER_CLASS)
public class Shape implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    protected int id
;
    @Embedded
    protected Transformation transformation
;
    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="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;
    }
    public Transformation getTransformation() {
        return transformation;
    }
    public void setTransformation(Transformation transformation) {
        this.transformation = transformation;
    }
}

 



Code:

package com
.codemiles.jpa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;

@
Entity
@Table(name="CIRCLE")
public class Circle extends Shape implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected float radius; 
    
@Embedded
    
@AttributeOverrides( {
        @AttributeOverride(name="scale", column=@Column(name="CIR_SACLE")),
        @AttributeOverride(name="rotate", column=@Column(name="ROT_SACLE"))
    })
    protected Transformation transformation;

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


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

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

  public Transformation getTransformation() {
        return transformation;
    }
    public void setTransformation(Transformation transformation) {
        this.transformation = transformation;
    }

}
 



@Embeddable class :
Code:

package com
.codemiles.jpa;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@
Embeddable
public class Transformation 
{
    protected int scale;
    protected float rotate;

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

 




_________________
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 : @Embeddable entity and @AttributeOverrides
 Get all objects for an entity     -  
 cmp entity beans     -  
 Find entity by id     -  
 JPA entity class example     -  
 secondary table per entity     -  
 JPA entity with table generator     -  
 @Transient annotation in JPA entity     -  
 @EntityListeners in your entity class     -  
 create a named query within entity class     -  



Topic Tags

Java JPA
cron





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