Enterprise Java Development@TOPIC@
Similar to @EmbeddedId except embedded object is not a primary key
Embedded object has no primary key -- housed in parent entity table
Embedded object represents some abstraction
Parent entity represents an abstraction with a primary key and the embedded object
The primary key could be the only thing the parent entity is providing
Figure 51.1. Embedded Object Example Database Schema
create table ORMCORE_XRAY ( id bigint not null, address varchar(255), XRAY_MAKER varchar(255), phone varchar(255), model varchar(255), primary key (id) )
Figure 51.2. Embedded Object Example Embedded Class
@Embeddable
public class Manufacturer {
private String name;
private String address;
private String phone;
Figure 51.3. Embedded Object Example Entity Class
@Entity
@Table(name="ORMCORE_XRAY")
public class XRay {
@Id
private long id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="name", column=@Column(name="XRAY_MAKER"))
//note that we are letting address and phone default
})
private Manufacturer maker;
private String model;
Figure 51.4. Embedded Object Example orm.xml
<entity class="ejava.examples.orm.core.mapped.XRay" access="FIELD">
<table name="ORMCORE_XRAY"/>
<attributes>
<id name="id"/>
<embedded name="maker">
<attribute-override name="name">
<column name="XRAY_MAKER"/>
</attribute-override>
<!-- address and phone will default to a column name -->
</embedded>
<!-- model will default to a column name -->
</attributes>
</entity>
<embeddable class="ejava.examples.orm.core.mapped.Manufacturer" access="FIELD"/>
Figure 51.5. Embedded Object Example Test
XRay xray3 = new XRay(3);
xray3.setModel("inside-counts");
xray3.setMaker(
new Manufacturer("hi-tech", "low valley", "410-555-1212"));
em.persist(xray3);
ID PHONE ADDRESS XRAY_MAKER MODEL -- ------------ ---------- ---------- ------------- 3 410-555-1212 low valley hi-tech inside-counts