I'm trying to undertand hibernate, so bear with me. I'm trying to use the ejb3 annotations.
Here's what I want to do:
I want to have multiple object types use the same metadata table to store their info. But I'd like for the metadata table to not care about how many other objects write to it.
Ex:
User object with demographics like name, address, etc.
Org object with demographics like address, phone etc.
I'd like to store those demographics in a meta data table, with the intention of adding other objects to it.
My thought is the metadata table would have a autogen id field as the PK, and an ownerId field as reference to the id from the object table user.id, bank.id) and a ownertype field which would be the class name of the object (com.blah.dao.user or com.blah.dao.org). And then all the other fields.
How do I do the annotations for this?
Here's what I have so far (extra removed for readability):
USER:
@Entity()
@SecondaryTable(name = "metadata", join = {@JoinColumn(name = "ownerid")})
public class User {
private Long id = null;
private Collection<MetaData> metaData = new ArrayList<MetaData>();
@Id(generate = GeneratorType.IDENTITY)
@Column(secondaryTable = "metadata")
public Long getId() {
return this.id;
}
@OneToMany(cascade = CascadeType.ALL, targetEntity = "com.blah.dao.MetaData")
@JoinColumn(name = "userId")
public Collection<MetaData> getMetaData() {
return metaData;
}
}
ORG:
@Entity()
@SecondaryTable(name = "metadata", join = {@JoinColumn(name = "ownerid")})
public class Org {
private Long id = null;
private Collection<MetaData> metaData = new ArrayList<MetaData>();
@Id(generate = GeneratorType.IDENTITY)
@Column(secondaryTable = "metadata")
public Long getId() {
return this.id;
}
@OneToMany(cascade = CascadeType.ALL, targetEntity = "com.blah.dao.MetaData")
@JoinColumn(name = "orgId")
public Collection<MetaData> getMetaData() {
return metaData;
}
}
METADATA:
@Entity(access = AccessType.FIELD)
public class MetaData {
private Long id = null;
private Long ownerId = null;
private String metaName = null;
@Column(length = 1024)
private String metaValue = null;
@Id(generate = GeneratorType.IDENTITY)
public Long getId() {
return this.id;
}
}
I have a feeling the secondary table thing isn't right, but I don't know what is. I don't have the ownertype field in the metadata class. Is that something hibernate can create for me? Do I have to manually put the class name in the field?
thanks!
|