Home » Eclipse Projects » EclipseLink » Relationship Inheritance(Entity inheritance for entity relationships.)
Relationship Inheritance [message #1690120] |
Wed, 25 March 2015 12:03 |
Kurt De Wit Messages: 1 Registered: March 2015 |
Junior Member |
|
|
Hello,
I want to setup a generic mechanism for linking GUI events based on a predefined key code (string representation) to a template used for generating documents on the fly. One table keeps track in the database of all GUIkeys and the corresponding template, which is kept in a separate table.
Since a template has some base properties, but additional information for a certain template might be possible, I have defined the following entity definitions.
@Entity
@Table(name = "t_template")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "templateType")
public abstract class TemplateData extends XXXXX{
@Column(name = "templateData", nullable = false)
@Lob
private byte[] data;
....
}
This class is the base class for two template types: mail and report template.
Both are defined as followed:
@Entity
@DiscriminatorValue(value = "REPORT")
public class ReportTemplate extends TemplateData {
....
}
@Entity
@DiscriminatorValue(value = "MAIL")
public class MailTemplate extends TemplateData {
....
}
So far, so good. When a instance of MailTemplate or ReportTemplate is persisted, they appear with the appropriate discriminator value in the discriminator column.
For the mapping, I have created a secondary object structure as follows.
A base mapping entity is the base class for both mailtemplate mapping and reporttemplate mapping entities.
@Entity
@Table(name="t_entitymapping")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "mappedType")
public abstract class EntityMapping<T extends xxxxx> extends yyyyy {
@Column(name = "mappedKey", nullable = false, unique = true)
private String key;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entityId")
private T entity;
Two subclasses of this base class exist.
@Entity
@DiscriminatorValue(value = "MAILTEMPLATEMAPPING")
@NamedQueries({
@NamedQuery(name = Resources.MAIL_TEMPLATE_FIND_BY_MAPPED_KEY,
query = "SELECT em " +
"FROM MailTemplateMapping em " +
"INNER JOIN FETCH em.entity t " +
"WHERE em.key = :mappedKey ")
})
public class MailTemplateMapping extends EntityMapping<MailTemplate> {
....
}
@Entity
@Table(name="t_reporttemplatemapping")
@NamedQueries({
@NamedQuery(name = Resources.REPORT_TEMPLATE_FIND_BY_MAPPED_KEY,
query = "SELECT em " +
"FROM ReportTemplateMapping em " +
"INNER JOIN FETCH em.entity t " +
"WHERE em.key = :mappedKey ")
})
public class ReportTemplateMapping extends EntityMapping<ReportTemplate> {
....
}
Uptill now this seem to work as expected. During persistance, the discrimininator values and corresponding properties are set in the database table as expected.
Problems arise when I want to retrieve information form the database.
Assume the namedquery as defined in the ReportTemplateMapping class.
Each time when this query is executed, the following SQL statement is generated at application server level:
SELECT t1.ID, t1.mappedType, t1.active, t1.createdBy, t1.createdOn, t1.mappedKey, t1.modifiedBy, t1.modifiedOn, t1.entityId, t1.outputFormat, t0.ID, t0.DTYPE, t0.active, t0.createdBy, t0.createdOn, t0.templateData, t0.description, t0.modifiedBy, t0.modifiedOn FROM t_template t0, t_entitymapping t1 WHERE (((t1.mappedKey = ?) AND (t1.mappedType = ?)) AND ((t0.ID = t1.entityId) AND (t0.DTYPE = ?)))
But no results are actually returned resulting in a NoResultException. When I print-out the bound values for the query, it get the following results.
bind => [REPORT_GLOBAL, ReportTemplateMapping, MailTemplate]
My question is, why does MailTemplate get filled in the query parameters while my SQL statement is querying the ReportTemplateMapping with the linked ReportTemplate as generic type. MailTemplate should be ReportTemplate to retrieve the correct results from the database. I am sure I must be doing something wrong in either the annotations used or either the EJB-QL query. I tried to add the Type(t) = ReportTemplate in the WHERE clause, but this doesn't seem to have any effect.
I could split up things in seperate tables and entities, but I rather prefer to group all template related and all mapping related data into a common database tables.
I am using Glassfish 4.1 with the bundled version of EclipseLink.
Any help is appreciated!
Kind regards,
Kurt
|
|
| |
Goto Forum:
Current Time: Sun Dec 22 06:09:03 GMT 2024
Powered by FUDForum. Page generated in 0.03603 seconds
|