Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've run into more problems than I hoped I would and as such I decided to dumb down the requirements for my first iteration. I'm currently trying to allow such <em>Extensions</em> only on the entire <em>Company</em> entity, in other words, I'm dropping the whole <em>Owner</em> requirement. So the problem could be rephrased as "How can I add <em>virtual columns</em> (entries in another entity that act like an additional column) to an entity at runtime?"</p> <p>My current implementation is as follow (irrelevant parts filtered out):</p> <pre><code>@Entity class Company { // The set of Extension definitions, for example "Location" @Transient public Set&lt;Extension&gt; getExtensions { .. } // The actual entry, for example "Atlanta" @OneToMany(fetch = FetchType.EAGER) @JoinColumn(name = "companyId") public Set&lt;ExtensionEntry&gt; getExtensionEntries { .. } } @Entity class Extension { public String getLabel() { .. } public ValueType getValueType() { .. } // String, Boolean, Date, etc. } @Entity class ExtensionEntry { @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "extensionId") public Extension getExtension() { .. } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "companyId", insertable = false, updatable = false) public Company getCompany() { .. } public String getValueAsString() { .. } } </code></pre> <p>The implementation as is allows me to load a <em>Company</em> entity and Hibernate will ensure that all its <em>ExtensionEntries</em> are also loaded and that I can access the <em>Extensions</em> corresponding to those <em>ExtensionEntries</em>. In other words, if I wanted to, for example, display this additional information on a web page, I could access all of the required information as follow:</p> <pre><code>Company company = findCompany(); for (ExtensionEntry extensionEntry : company.getExtensionEntries()) { String label = extensionEntry.getExtension().getLabel(); String value = extensionEntry.getValueAsString(); } </code></pre> <p>There are a number of problems with this, however. Firstly, when using FetchType.EAGER with an @OneToMany, Hibernate uses an outer join and as such will return duplicate Companies (one for each ExtensionEntry). This can be solved by using Criteria.DISTINCT_ROOT_ENTITY, but that in turn will cause errors in my pagination and as such is an unacceptable answer. The alternative is to change the FetchType to LAZY, but that means that I will always "manually" have to load <em>ExtensionEntries</em>. As far as I understand, if, for example, I loaded a List of 100 <em>Companies</em>, I'd have to loop over and query each of those, generating a 100 SQL statements which isn't acceptable performance-wise.</p> <p>The other problem which I have is that ideally I'd like to load all the <em>Extensions</em> whenever a <em>Company</em> is loaded. With that I mean that I'd like that @Transient getter named getExtensions() to return all the <em>Extensions</em> for any <em>Company</em>. The problem here is that there is no foreign key relation between <em>Company</em> and <em>Extension</em>, as <em>Extension</em> isn't applicable to any single <em>Company</em> instance, but rather to all of them. Currently I can get past that with code like I present below, but this will not work when accessing referenced entities (if for example I have an entity <em>Employee</em> which has a reference to <em>Company</em>, the <em>Company</em> which I retrieve through employee.getCompany() won't have the <em>Extensions</em> loaded):</p> <pre><code>List&lt;Company&gt; companies = findAllCompanies(); List&lt;Extension&gt; extensions = findAllExtensions(); for (Company company : companies) { // Extensions are the same for all Companies, but I need them client side company.setExtensions(extensions); } </code></pre> <p>So that's were I'm at currently, and I have no idea how to proceed in order to get past these problems. I'm thinking that my entire design might be flawed, but I'm unsure of how else to try and approach it.</p> <p>Any and all ideas and suggestions are welcome!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload