Hi, I am developing a utility (something that will be packaged in a JAR to be used by third-party applications). That utility defines entities for item recommendations and tagging, without knowing exactly what entity is being recommended or tagged.
So we have something like this:
Code:
@Entity
public class Tag {
@Id
@GeneratedValue
private Long id;
private String tagName;
@OneToMany
private Collection<TagAssignment> assignments;
// Other parts of the class omitted.
}
Code:
@Entity
public class TagAssignment {
@Id
@GeneratedValue
private Long id;
@AnyMetaDef(???)
@Any(metaColumn=@Column(name="target_entity"), fetch=FetchType.EAGER)
private Object target;
@ManyToOne
private Tag tag;
// Other parts of the class omitted.
}
The problem is the @AnyMetaDef annotation. We don't know how to fill it because we don't know what type of entities the end user will have. It is a requeriment that we should be able to map that even if the target entities have different primary key types, including composite ones. A further requeriment is that the end user does not to know Java, so an approach where the XML files are used is preferred (it could be even better if we could completely avoid the XMLs too). If possible, generating some sort of meta information at runtime before the EntityManagerFactory is created is the best for us, as we already are creating an Ejb3Configuration object at runtime during the initialization and configuring properties and registering the entities at runtime on that object, so that would be the finest place to define the Any mapping data.
So, what sort of approach should I try to solve this problem? Is it possible to define the Any metadata using the Ejb3Configuration? If yes, how? If not, how do define them in XMLs? Is it possible to be modelled even if the entities' primary key types are different?
Actually, we are handling that with an embedded class called GenericReference which persist the target entity class name and a serialized version of it's Id. However, we must do this by hand and we are getting some issues, but this is something that Hibernate for sure could/should automate, as this is the purpose of the @Any annotation.