-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: DetachedCriteria and SQL Syntax Exception (Unknown column)
PostPosted: Sat May 03, 2008 12:21 pm 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
Hello guys,

I am trying to build my first detachedCritera and does get following exception every time:

Code:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'bi1_.name' in 'where clause'
   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
   com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)

The column name in my table clothes is present.

I want to get all persons, which don't have any shirts in their clothes list:

Code:
      List<Person> result = new ArrayList<Person>();
      _session = ((HibernateEntityManager) em).getSession();
      Criteria crit = _session.createCriteria(Person.class);
      
      DetachedCriteria noShirts=
          DetachedCriteria.forClass(Person.class)
              .createAlias("clothes ", "bi")
              .add(Restrictions.eq("bi.name", "shirt"))
              .setProjection(Projections.id());

      crit.add(Subqueries.notExists(noShirts));    
      
      result = crit.list();   


Does anybody has an idea what I am doing wrong? Or is there any other solution? Restriction.ne(...) does not work in this case, because I got a list of clothes in my entity person.

Thank you very much!


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 03, 2008 3:24 pm 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
it is bug in hibernate

http://opensource.atlassian.com/project ... se/HHH-925


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 03, 2008 4:54 pm 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
ferozz wrote:


I found that too. But this bug is from 2007. I do think, that this bug has been fixed already.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 3:32 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
I did try following: (Fluke... gave that tip to me)

Code:
List<Person> result = new ArrayList<Person>();
_session = ((HibernateEntityManager) em).getSession();

Criteria crit = _session.createCriteria(Person.class, "con");
      
DetachedCriteria buy =
   DetachedCriteria.forClass(Person.class, "dc")
   .add(Restrictions.eq("dc.id", "con.id"))
   .createAlias("clothes", "bi")
   .add(Restrictions.eq("clothes.name", "shirt"))
   .setProjection(Projections.id());

crit.add(Subqueries.notExists(buy));



But know I get an exception, that a string could not be converted to an Int..?!

Code:
root cause

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
   org.hibernate.type.IntegerType.set(IntegerType.java:41)
   org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
   org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
   org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1707)
   org.hibernate.loader.Loader.bindParameterValues(Loader.java:1678)
   org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
   org.hibernate.loader.Loader.doQuery(Loader.java:673)
   org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
   org.hibernate.loader.Loader.doList(Loader.java:2220)
   org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
   org.hibernate.loader.Loader.list(Loader.java:2099)
   org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
   org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
   org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
   de.session.ReportServiceBean.justATest(ReportServiceBean.java:162)
   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   java.lang.reflect.Method.invoke(Unknown Source)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
   org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
   org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
   org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.aspects.security.RoleBasedAuthorizationInterceptor.invoke(RoleBasedAuthorizationInterceptor.java:166)
   org.jboss.ejb3.security.RoleBasedAuthorizationInterceptor.invoke(RoleBasedAuthorizationInterceptor.java:108)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
   org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:106)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:214)
   org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:184)
   org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:81)
   $Proxy121.justATest(Unknown Source)
   de.mbeans.ReportManagerBean.createCSVFile(ReportManagerBean.java:167)
   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   java.lang.reflect.Method.invoke(Unknown Source)
   org.apache.el.parser.AstValue.invoke(AstValue.java:131)
   org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
   com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
   javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
   com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
   javax.faces.component.UICommand.broadcast(UICommand.java:383)
   org.ajax4jsf.framework.ajax.AjaxViewRoot.processEvents(AjaxViewRoot.java:180)
   org.ajax4jsf.framework.ajax.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:158)
   org.ajax4jsf.framework.ajax.AjaxViewRoot.processApplication(AjaxViewRoot.java:346)
   com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
   com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
   com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
   javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
   de.filter.SecureRedirectFilter.doFilter(SecureRedirectFilter.java:35)
   org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:147)
   org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:127)
   org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:277)
   org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)




The mapping is working. All other queries (without detachedCriteria) are working.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 4:24 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
UPDATE: Error solved now!

I had to make an alias to my entity clothes in the main criteria too. This alias must be the same alias as in my detachedCriteria Object.

But I got still the same problem:
In the example above I do want to get all persons, which dont have any shirts in their clothes list. But I still do get persons, which have among others shirts. :-(

Does anybody know any other solution to get this restriction?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 5:17 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not sure but I think that restriction is wrong which results in an empty result set which makes the non-exists always true:

Code:
.add(Restrictions.eq("dc.id", "con.id"))


this should be

Code:
.add(Restrictions.eqProperty("dc.id", "con.id"))


or whatever it's in java ... in C# its EqProperty. In the other thread the correct restriction is used, so maybe that's just a typo here.

If it's really a typo, you may want to try a detached criteria on Clothes instead of Person. Assuming you have property for the person on Clothes:

Code:
DetachedCriteria buy =
   DetachedCriteria.forClass(Clothes.class, "bi")
   .add(Restrictions.eq("bi.personId", "con.id"))
   .add(Restrictions.eq("bi.name", "shirt"))
   .setProjection(Projections.id());



You can also try to use the alias here instead of the class:

Code:
   .add(Restrictions.eq("clothes.name", "shirt"))
->
   .add(Restrictions.eq("bi.name", "shirt"))


Sometimes I had problems with my queries, when I specified an alias and didn't use it consequently.

If all of this does not help, post the generated sql statement.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 5:59 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
Hi wolli,

I did never use the first restriction. That was a wrong copy & caste from me =)
I do have a many-to-many relationship between person and clothes. There are no ID's that are matching.

I tried following now:

Code:
Criteria crit = _session.createCriteria(Person.class, "con");
      
DetachedCriteria buy =
   DetachedCriteria.forClass(Clothes.class, "dc")
   .add(Restrictions.eq("dc.name", "shirt"))
   .setProjection(Projections.id());

crit.add(Subqueries.propertyNe("clothes", buy));


I do get as result all Persons independent of their clothes.

I think I have to find out first, how I can see the generated SQL statements.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 6:50 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Put hibernate.show_sql=true in your configuration and make sure that logging is enabled.

Can you post your mappings ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 8:31 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
wolli wrote:
Put hibernate.show_sql=true in your configuration and make sure that logging is enabled.

Can you post your mappings ?


I am using JBoss with log4j. I had to search for the xml configuration.
The real example is a bit more complicated. I have Contacs (instead of Persons), which have differen buyinginterests (for example services).

Code:
Criteria crit = _session.createCriteria(Contact.class, "con");

DetachedCriteria buy =
   DetachedCriteria.forClass(Contact.class, "dc")
      .createAlias("buyinginterest", "buyinginterest")
      .add(Restrictions.eq("buyinginterest.name", "services"))
      .setProjection(Projections.id());
crit.createAlias("buyinginterest", "buyinginterest");
crit.add(Subqueries.notExists(buy));

Generated SQL:
select ...... where not exists (select this0__.id as y0_ from contact this0__ where buyinginte1_.name=?)


Code:
Criteria crit = _session.createCriteria(Contact.class, "con");
DetachedCriteria buy =
   DetachedCriteria.forClass(Buyinginterest.class, "dc")
      .add(Restrictions.eq("dc.name", "services"))
      .setProjection(Projections.id());

crit.createAlias("buyinginterest", "buyinginterest");
crit.add(Subqueries.propertyNotIn("buyinginterest" ,buy));

Generated SQL Statement:
where this_.id not in (select this0__.id as y0_ from buyinginterest this0__ where this0__.name=?)



The entity classes:
Code:
@Entity
@Table(name = "contact")
public class Contact implements Serializable, Comparable<Contact> {

   private static final long serialVersionUID = 1L;
   
   @Id
   @Column(name="id")
   @GeneratedValue
   private int id;
   
   ...
   ...
   ...
   
   @ManyToMany
   @JoinTable(name="contact_buyinginterest", joinColumns = @JoinColumn(name="contact_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name="buyinginterest_id", referencedColumnName = "id"))
   private List<Buyinginterest> buyinginterest;
   
   }


Code:

@Entity
@Table(name="buyinginterest")
public class Buyinginterest implements Serializable {
   
   @Id
   private int id;

   @Lob
   private String description;

   private String name;

   private static final long serialVersionUID = 1L;

   public Buyinginterest() {
      super();
   }


Thanks very much for help![/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 8:58 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You want all contacts that are NOT interested in buying "services", right ?

Code:
Criteria crit = _session.createCriteria(Contact.class, "con");

DetachedCriteria buy =
   DetachedCriteria.forClass(Contact.class, "dc")
      .add(Restrictions.eqProperty("dc.id", "con.id"))
      .createAlias("buyinginterest", "buyinginterest")
      .add(Restrictions.eq("buyinginterest.name", "services"))
      .setProjection(Projections.id());

crit.add(Subqueries.propertyNotIn("id", buy));


If you put the contact id on your Buyinginterest class, it should be possible, to make this query a bit easier and probably faster:

Code:
DetachedCriteria buy =
   DetachedCriteria.forClass(Buyinginteret.class, "bi")
      .add(Restrictions.eqProperty("bi.contactId", "con.id"))
      .add(Restrictions.eq("bi.name", "services"))
      .setProjection(Projections.id());

crit.add(Subqueries.Exists(buy));

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 9:08 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
[quote="wolli"]You want all contacts that are NOT interested in buying "services", right ?

Yes thats right!

If you put the contact id on your Buyinginterest class, it should be possible, to make this query a bit easier and probably faster:

Thats impossible. If I would put the contact_id in my buyinginterest class, I would destroy the many to many relation. One Contact can have Many Buyinginterests and One Buyinginterest can have Many Contacts. The relation is in table contact_buyinginterest.

I cant really understand why this is not working:
Code:
crit.add(Subqueries.propertyNotIn("buyinginterest" ,buy));

where this_.id not in (select this0__.id as y0_ from buyinginterest this0__ where this0__.name=?)


I do think, that with this_.id the contact_id is mentioned and with this0_.id the buyinginterest_id. In this case, there will be compared two absolutely different id's.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 9:16 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Code:
DetachedCriteria buy =
   DetachedCriteria.forClass(Contact.class, "dc")
      .add(Restrictions.eqProperty("dc.id", "con.id"))
      .createAlias("buyinginterest", "buyinginterest")
      .add(Restrictions.eq("buyinginterest.name", "services"))
      .setProjection(Projections.id());


This criteria should return contact ids ! So you have to use the contact id in the subquery:

Code:
crit.add(Subqueries.propertyNotIn("id" ,buy));


Run the detached criteria alone and see what is returned.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 9:59 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
You are right. The DetachedCriteria gives me list of contacts, which does have a buyinginterest "services". The projection is only there because of some error messages about an missing projection.

Code:
crit.add(Subqueries.propertyNotIn("con.id", buy));

where this_.id not in (select this0__.id as y0_ from contact this0__ where this0__.id=this_.id and buyinginte1_.name=?)


And I still do get Contacts, which have services as buyinginterest...

UPDATE: This generated statement seems to be right:
Code:
Criteria crit = _session.createCriteria(Contact.class, "con");
      
DetachedCriteria buy =
   DetachedCriteria.forClass(Contact.class, "dc")
   .createAlias("buyinginterest", "buyinginterest")
   .add(Restrictions.eq("buyinginterest.name", "services"))
   .setProjection(Projections.id());

crit.createAlias("buyinginterest", "buyinginterest");
crit.add(Subqueries.propertyNotIn("con.id", buy));

where this_.id not in (select this0__.id as y0_ from contact this0__ where buyinginte1_.name=?)

--> Get all contacts which are not in the select statement above. But I still get wrong results.


Last edited by thinkpad on Mon May 05, 2008 10:07 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 10:05 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Is that the complete statement that was generated ? The join to buyinginteres is missing ??

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 10:10 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
wolli wrote:
Is that the complete statement that was generated ? The join to buyinginteres is missing ??


No there are about 10 or more joins because of dependencies :-)
THe where-clause should only be interesting...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.