Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Relationship Inheritance(Entity inheritance for entity relationships.)
Relationship Inheritance [message #1690120] Wed, 25 March 2015 12:03 Go to next message
Kurt De Wit is currently offline Kurt De WitFriend
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
Re: Relationship Inheritance [message #1691361 is a reply to message #1690120] Mon, 06 April 2015 15:19 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
I never really liked generics, as it seems to be confusing the shared mapping in the EntityMapping class. All subclasses inherit this mapping, sharing the type as well, and it looks like the due to order they are loaded, the EntityMapping<MailTemplate> is winning.
Try setting the targetEntity within the ManyToOne mapping:
@ManyToOne(fetch = FetchType.EAGER, targetEntity = TemplateData.class)
Previous Topic:Is it possible to weave a .class file programmatically
Next Topic:Two persistence units share same cache
Goto Forum:
  


Current Time: Sun Dec 22 06:09:03 GMT 2024

Powered by FUDForum. Page generated in 0.03603 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top