-->
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.  [ 1 post ] 
Author Message
 Post subject: [Hibernate 4.1.7] Criteria on EmbeddedId
PostPosted: Fri Jan 24, 2014 10:11 am 
Newbie

Joined: Fri Jan 24, 2014 10:00 am
Posts: 1
I have a problem with creating Criteria search result with Entity that has EmbeddedId.
Let's say I have many-to-many relation with additional fields. My example is to connect Player with Season as many-to-many relationship (called Performance) with additional field : goals. Example here: https://www.dropbox.com/s/3f79euvlth9z1op/erd.png

So I have mapped Entities:

Code:
@Entity
@Table(name="performances")
@AssociationOverrides({
   @AssociationOverride(name="id.player", joinColumns=@JoinColumn(name="player_id")),
   @AssociationOverride(name="id.season", joinColumns=@JoinColumn(name="season_id"))
})
public class Performance implements Serializable {

   private static final long serialVersionUID = 1L;
   @EmbeddedId
   @Getter @Setter
   private PerformanceID id;
   @Getter @Setter
   private int goals;
   public Player getPlayer(){
      return id.getPlayer();
   }
   public Season getSeason(){
      return id.getSeason();
   }

   // equals, hashcode, tostring...
}


Code:
@Embeddable
public class PerformanceID implements Serializable {

   private static final long serialVersionUID = 1L;
   @ManyToOne(fetch=FetchType.LAZY, targetEntity=Player.class)
   @JoinColumn(name="player_id")
   @Getter @Setter
   private Player player;
   @ManyToOne(fetch=FetchType.LAZY, targetEntity=Season.class)
   @Getter @Setter
   private Season season;   

}

Now I want to search my objects: I want to search Performance that hawe Players with name "Claudio". In HQL ewerything works fine:
Code:
public List<Performance> customList() {
  Query query = sessionFactory.getCurrentSession().createQuery("from Performance where id.player.name = 'Claudio'");
  return query.list();
   }

It generates SQL like:
Quote:
select performanc0_.player_id as player2_1_, performanc0_.season_id as season3_1_, performanc0_.goals as goals1_ from performances performanc0_ cross join players player1_ where performanc0_.player_id=player1_.id and player1_.name='Claudio'

But i cannot generate it by create criteria. My try was:
Code:
public List<Performance> customList() {
      Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Performance.class);
      criteria.createAlias("id.player", "player");
      criteria.add(Restrictions.ilike("player.name", "Claudio"));
      return criteria.list();
   }

and it has generated sql like:
Quote:
select this_.player_id as player2_1_0_, this_.season_id as season3_1_0_, this_.goals as goals1_0_ from performances this_ where player1_.name ilike ?

But it failed with SQLGrammarException (missing claule "FROM" for table "player1_"):
My stacktrace:
Quote:
[org.hibernate.exception.SQLGrammarException: BŁĄD: brakująca klauzula FROM dla tabeli "player1_"
Pozycja: 129
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy30.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1897)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1698)
at org.hibernate.loader.Loader.doQuery(Loader.java:832)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293)
at org.hibernate.loader.Loader.doList(Loader.java:2382)
at org.hibernate.loader.Loader.doList(Loader.java:2368)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2198)
at org.hibernate.loader.Loader.list(Loader.java:2193)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:122)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1618)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
at pl.andrzejcichon.criterionlazydatamodel.repository.impl.PerformanceDatabaseRepository.customList(PerformanceDatabaseRepository.java:30)
at pl.andrzejcichon.criterionlazydatamodel.repository.impl.PerformanceDatabaseRepositoryTest.test(PerformanceDatabaseRepositoryTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.postgresql.util.PSQLException: BŁĄD: brakująca klauzula FROM dla tabeli "player1_"
Pozycja: 129
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 43 more


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

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.