I was having trouble getting
@AttributeOverrides to work correctly for an
@Embeddable object, but I figured out the problem. In my attribute for the
@Embeddable object, the method order was set/get instead of get/set. When I fixed that, it started working.
Yes, I could have annotated the private members, but I chose to use annotations on the public accessor methods instead, and thus ran into this problem.
This is my
@Embeddable class:
Code:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class Embedded1 implements Serializable {
private int valueX;
private int valueY;
public Embedded1() {
}
public int getValueX() {
return this.valueX;
}
public void setValueX(int valueX) {
this.valueX = valueX;
}
public int getValueY() {
return this.valueY;
}
public void setValueY(int valueY) {
this.valueY = valueY;
}
}
And here is my first attempt to embed that in another class:
Code:
import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.AttributeOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "entity1")
public class Entity1 implements Serializable {
private long id;
private Embedded1 embedded1;
public Entity1() {
}
@Id @GeneratedValue
@Column(name = "entity1_id")
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Embedded
@AttributeOverrides
({
@AttributeOverride(name="valueX",
column=@Column(name="entity1_valueX")),
@AttributeOverride(name="valueY",
column=@Column(name="entity1_valueY")) })
public void setEmbedded1(Embedded1 embedded1) {
this.embedded1 = embedded1;
}
public Embedded1 getEmbedded1() {
return this.embedded1;
}
}
This doesn't work, and produces the following SQL code to create the table:
Code:
create table entity1 (
entity1_id bigint generated by default as identity (start with 1),
valueX integer not null,
valueY integer not null,
primary key (entity1_id)
);
I changed my
@Entity class, so that the attribute methods accessing the
@Embeddable object were the opposite order:
Code:
@Embedded
@AttributeOverrides
({
@AttributeOverride(name="valueX",
column=@Column(name="entity1_valueX")),
@AttributeOverride(name="valueY",
column=@Column(name="entity1_valueY")) })
public Embedded1 getEmbedded1() {
return this.embedded1;
}
public void setEmbedded1(Embedded1 embedded1) {
this.embedded1 = embedded1;
}
and it now produces the correct SQL code:
Code:
create table entity1 (
entity1_id bigint generated by default as identity (start with 1),
entity1_valueX integer,
entity1_valueY integer,
primary key (entity1_id)
);