Code subject: @Embeddable entity and @AttributeOverrides
Posted: Mon Apr 26, 2010 1:00 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 time
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; } }