-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Filter and duplicate column error
PostPosted: Mon Apr 18, 2011 7:11 am 
Newbie

Joined: Mon Apr 18, 2011 6:51 am
Posts: 4
Hi,

i want to user the Hibernate Filter feature to separate the data of different tenants.
Therefor i created a base class TenantEntity.

Code:
@FilterDef(name = "tenantFilter",
    defaultCondition = "_tenantId = :tenantId",
    parameters = @ParamDef(name = "tenantId", type = "long"))
@Filter(name = "tenantFilter")
@MappedSuperclass
public class TenantEntity extends BaseEntity {
    ...
    private Tenant tenant;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "_tenantId")
    public Tenant getTenant() {
        return tenant;
    }
}


The problem is that for some mappings i get an "Column '_tenantId' in where clause is ambiguous" error

For example for the following code where the class TemplateConfiguration has a
one to many association with the class TemplatePage.

Code:
@Entity
@Table(name = "templateconfigurations")
public class TemplateConfiguration extends TenantEntity {
    ...
   private List<TemplatePage> pages = new ArrayList<TemplatePage>();

   @OneToMany(mappedBy = "configuration", fetch = FetchType.EAGER)
   @Cascade(CascadeType.ALL)
   public List<TemplatePage> getPages() {
       return pages;
    }
}



Code:
@Entity
@Table(name = "templatepages")
public class TemplatePage extends TenantEntity {
   ...
   private TemplateConfiguration configuration;

   @ManyToOne
   @JoinColumn(name = "_configurationId")
   public TemplateConfiguration getConfiguration() {
       return configuration;
   }
}


In this case it would not be possible to use another superclass for the TemplatePage without
any tenant informations. Because i also want to query the TemplatePage's.

Another simple is a class LogEntry that has a recursive one to many association:

Code:
@Entity
@Table(name = "logentries")
public class LogEntry extends TenantEntity {
   ...
   private List<LogEntry> childEntries = new ArrayList<LogEntry>();

   @OneToMany(mappedBy = "parentEntry")
   @Cascade(CascadeType.ALL)
   public List<LogEntry> getChildEntries() {
       return childEntries;
   }
}


Top
 Profile  
 
 Post subject: Re: Hibernate Filter and duplicate column error
PostPosted: Tue Apr 19, 2011 6:41 am 
Newbie

Joined: Mon Apr 18, 2011 6:51 am
Posts: 4
Hi,

after further investigation i found out that the problem arises because the TemplatePages are
loaded with an outer join and the result set contains the _tenantId columns of both tables.

A workaroung would be using defaultCondition = "this_._tenantId = :tenantId" instead of
defaultCondition = "_tenantId = :tenantId".

This solves the duplicate column problem, but the disadvantage is that all queries must be
executed with the criteria API, or every HQL-Query mus use "this_" as alias.

A solution would be if the following would work:
defaultCondition = "{alias}._tenantId = :tenantId
but unfortunately it does not!

So is there any way to get the current alias in the condition?


Top
 Profile  
 
 Post subject: Re: Hibernate Filter and duplicate column error
PostPosted: Mon Sep 15, 2014 8:24 pm 
Newbie

Joined: Mon Sep 15, 2014 8:11 pm
Posts: 1
Hi Swen,

I'm just curious if you came up with a better solution at all?

Also, you said that {alias} didn't work for you, but did you set deduceAliasInjectionPoints=false when using that notation?

I'm dealing with a slightly different problem:
* I'm issuing an HQL query, inner joining with two tables, both of which have the same column name that I have a Hibernate Filter for (table1.foo, table2.foo).
* I'm getting the "column name ambiguous" error because the filter is not fully qualified ("... where foo='bar'" rather than "... where table1.foo='bar'") . I could prefix it with "this_" which produces "... where this_.foo='bar'" but then I want the Hibernate filter to be applied to the second (joined) table as well. Why isn't it being applied there?
* Is this a "feature" of Hibernate filters, that if I'm not using a collection for the join, any HQL with joins will not have filters applied to them? Ultimately I want to see: "... where table1.foo='foo' and table2.foo='bar'"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.