@OneToMany with @JoinTable and @Embedded
I have 3 tables representing a container-containee relationship like this:
create table Containee(id int, name varchar(100), primary key(id));
create table Container(id int, name varchar(100), sourceId int, sourceType
varchar(100), primary key(id));
create table JoinTable(containeeId int, resourceId int, resourceType
varchar(100), primary key(containeeId, resourceId, resourceType));
The hibernate entities are mapped as follows
@Entity
public class Containee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Basic
private String name;
}
@Entity
public class Container implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Basic
private String name;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "sourceId")),
@AttributeOverride(name = "type", column = @Column(name = "sourceType"))
})
private DomainObject domainObject;
@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name = "JoinTable",
joinColumns = {
@JoinColumn(name="resourceId", referencedColumnName = "sourceId"),
@JoinColumn(name="resourceType", referencedColumnName = "sourceType")
},
inverseJoinColumns = @JoinColumn(name = "containeeId")
)
private Collection<Containee> containees;
}
The embedded class is declared as
@Embeddable
public class DomainObject {
private int id;
private String type;
public int getId() {
return id;
}
public String getType() {
return type;
}
}
The above code doesn't work and I get the following error:
referencedColumnNames(sourceId, sourceType) of containees referencing
Container not mapped to a single property.
If I however remove @Embedded domainObject field and replace it with 2
@Basic sourceId and sourceType, the same code works like a charm. I have
tried numerous things but nothing seems to work with the @Embedded field
too. Any help is appreciated!
No comments:
Post a Comment