I have a bit of a catch-22 that I need some help wrapping my head around. I have two EAR applications, 'Product' and 'Manufacturer'. I also have a 'Common' library jar with a bunch of shared code.To avoid circular dependencies I don't want either application to directly depend on the other. To do this, I have abstract @MappedSuperclass versions of my domain classes (also called Product and Manufacturer) in the common library. So far so good.
The problem comes when I need to define some relationships between the classes. The child version of Product has a 'primary manufacturer' property defined like this:
@ManyToOne
@JoinColumn(name = "primary_mfg_id")
protected common.domain.Manufacturer primaryMfg;
and a collection of manufacturers:
@ContainedIn
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "products_to_manufacturers", inverseJoinColumns = {
@JoinColumn(name = "manufacturer_id", referencedColumnName = "id") }, joinColumns = {
@JoinColumn(name = "product_id", referencedColumnName = "id") })
@OrderBy("name")
@AuditJoinTable(inverseJoinColumns = { @JoinColumn(name = "manufacturer_id", referencedColumnName = "id") })
protected Set<common.domain.Manufacturer> manufacturers;
If I try to deploy that I will get an error saying that @ManyToOne references an unknown entity type. That makes sense, Manufacturer is a MappedSuperClass not an entity. Unfortunately, I can't think of a way to solve this without major headaches. I could combine the parent and child Manufacturer classes, but that would require me to also move a good portion of the domain model for the Manufacturer application into the common jar. I could have a Product specific version of the Manufacturer class but that would require adding a DTYPE to the table that is totally meaningless outside of hibernate.
I feel like there must be a cleaner way to structure this code, but I'm all out of ideas. Any help would be appreciated.
Update: I was able to work around the 'primaryMfg' property by changing it to an id and looking up the manufacturer via the entity manager when I need the full object. Unfortunately, I can't figure out a way to do that for the 'manufacturers' property so I am still stuck.
Update 2: Using the ORM mapping solved the compile issues, but there are still runtime problems. Those issues go beyond the scope of the original question so I have posted another question here: Getting An Instance Of A Mapped Superclass With Hibernate EntityManager