@Embeddable annotation is used on a class whose instances are stored as a part of an owning entity. Each of the persistent properties or fields of the embedded object is mapped to the database table for the owner entity.

@Entity
public class MyEntity {
@Id
@GeneratedValue
private int id;
@Embedded
private ClassB classBRef;
.............
}
Using @Embedded in an @Embeddable:

package com.logicbig.example;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
@Embeddable
public class ClassB {
private String myStrB;
@Embedded
private ClassA classARef;
.............
}

@Embeddable
public class ClassA {
private String myStr;
private int myInt;
.............
}
Original Post@Embeddable with @ManyToOne relationship

package com.logicbig.example;
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
@Embeddable
public class ClassA {
private String myStr;
@ManyToOne
private EntityB entityBRef;
public String getMyStr() {
return myStr;
}
public void setMyStr(String myStr) {
this.myStr = myStr;
}
public EntityB getEntityBRef() {
return entityBRef;
}
public void setEntityBRef(EntityB entityBRef) {
this.entityBRef = entityBRef;
}
@Override
public String toString() {
return "ClassA{" +
"myStr='" + myStr + '\'' +
", entityBRef=" + entityBRef +
'}';
}
}
Original Post