tl;dr;
I don't know if I'm reading the JPA 2.1 spec wrong[1],
but can I use @Embedded FooClass fooClass; in two separate
entities? If so, what are the restrictions?
Long winded:
Basically, different tables in my database have
semantically equivalent sets of columns that I'll call
patterns. USERSTAMP/TIMESTAMP is a popular one. So is
CREATE_BY/CREATE_DATE. Some tables have multiple patterns
Some patterns have sub patterns.
Option 1: Copy all the code to each class; and use java
interfaces to interact with them. I don't want to do this
because (1) copying code is bad mmkay and (2) interfaces
don't seem to have a home in the JPA 2.1 spec.
Option 2: Use subclasses. I don't want to do this because
(1) some tables have multiple overlapping/nonoverlapping
(multiple inheritance) patterns and (2) IS-A is hardly a
reasonable way to describe most of them).
Option 3: Use @Embeddable?
I'm wondering if it's a viable design to use @Embeddable
for these, keeping the functionality common across classes
in my data model. It doesn't appear that the JPA 2.1 spec
would support this pattern if only because of specifically
leaving out embedded classes from having @PrePresist
methods.[2]
Specifically, I'm running into an issue on deploy (many
many times over) when the pattern contains an update-able
@ManyToOne uni-directional relationship.
Versions tested: eclipselink 2.5.0 from maven, and a
nightly download of 2.5.1
Error Received: EclipseLink-93
Example log excerpt:
Exception [EclipseLink-93] (Eclipse Persistence
Services - 2.5.1.v20130824-981335c):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [COMMENT_HAVING_FOO]
is not present in this descriptor.
Descriptor: RelationalDescriptor(CommentDAO -->
[DatabaseTable(COMMENTS)])
Exception [EclipseLink-93] (Eclipse Persistence
Services - 2.5.1.v20130824-981335c):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [COMMENT_HAVING_BAR]
is not present in this descriptor.
Descriptor: RelationalDescriptor(CommentDAO -->
[DatabaseTable(COMMENTS)])
Exception [EclipseLink-93] (Eclipse Persistence
Services - 2.5.1.v20130824-981335c):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [COMMENT_HAVING_FBAR]
is not present in this descriptor.
Descriptor: RelationalDescriptor(CommentDAO -->
[DatabaseTable(COMMENTS)])
I can supply a small test case on request, but like I
said, I don't even know if this is a valid JPA design
pattern.
Does anyone have any pointers for me? I'm rather new to
JPA 2.1 and eclipselink.
I am willing to submit tests/patches to extend
eclipselink to allow this. (fix this?)
Thanks for reading!
William
[1] Last sentence of Section 2.5 is "Attempting to share
an embedded object across entities has undefined semantics"
I really don't understand why this sentence is there to
begin with: you change an object referenced by two entities
should have the same effect as any other time you do that.
[2] First sentence of Section 3.5.2 is "Entity lifecycle
callback methods can be defined on an entity listener class
and/or directly on an entity class or mapped superclass."
Which is unfortunate because this means I'd have to add
annotations to each entity that contains any given pattern
that I would need a callback on and/or add reflection to a
default entity listener.