Bug 321 : Incorrect table alias with Multiple OneToOne on bean
Priority 
High
Reported Version 
 
Logged By 
Rob
Status 
Fixed
Fixed Version 
2.7.0
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
18/09/2010
Updated 
18/09/2010
Type 
Bug
 
Attachments 
No attachments

As per http://groups.google.com/group/ebean/browse_thread/thread/af121ba6e7234849

Hi, I have a problem fetching a bean containing other beans; here is
some code:

@Entity
public class Abitazione implements Serializable {
@Id private Integer id;
@OneToMany(cascade=CascadeType.ALL, mappedBy="abitazione")
@PrivateOwned List pratiche;
@Version int updateCount;
...

}

@Entity
public class Pratica implements Serializable {
@Id private Integer id;
@ManyToOne(optional=false) private Abitazione abitazione;
@OneToOne(cascade=CascadeType.ALL, optional=true, mappedBy="pratica")
@PrivateOwned private ConformitaEdilizia conformita;
@OneToOne(cascade=CascadeType.ALL, optional=true, mappedBy="pratica")
@PrivateOwned private ParereIgienicoSanitario parere;
@Version private int updateCount;
...

}

@Entity
public class ParereIgienicoSanitario implements Serializable {
@Id private Integer id;
@OneToOne(optional=false) private Pratica pratica;
@Version private int updateCount;
...

}

@Entity
public class ConformitaEdilizia implements Serializable {
@Id private Integer id;
@OneToOne(optional=false) private Pratica pratica;
@Version private int updateCount;
...

}

So a bean of class Abitazione has a list of beans of class Pratica.
Pratica has one ParereIgienicoSanitario and one ConformitaEdilizia.

If I try to load a bean of class Abitazione ad read the list of
Pratica:

abitazione = Ebean.find(idoneitaalloggiativa.model.Abitazione.class,
100);

for (idoneitaalloggiativa.model.Pratica pratica :
abitazione.getPratiche()) {
System.out.println(pratica);
System.out.println(pratica.getConformita());
System.out.println(pratica.getParere());

}

I get an error:

Exception in thread "main" javax.persistence.PersistenceException:
Query threw SQLException:Column "AA.ID" not found; SQL statement:
select a.id c0
, ap.id c1, ap.update_count c2, ap.abitazione_id c3, apc.id
c4, aa.id c5
from ABITAZIONE a
left outer join PRATICA ap on ap.abitazione_id = a.id
left outer join CONFORMITA apc on apc.pratica_id = ap.id
left outer join PARERE app on app.pratica_id = ap.id
where a.id = ?
order by a.id [42122-140 begin_of_the_skype_highlighting 42122-140 end_of_the_skype_highlighting] Query was:
select a.id c0
, ap.id c1, ap.update_count c2, ap.abitazione_id c3, apc.id
c4, aa.id c5
from ABITAZIONE a
left outer join PRATICA ap on ap.abitazione_id = a.id
left outer join CONFORMITA apc on apc.pratica_id = ap.id
left outer join PARERE app on app.pratica_id = ap.id
where a.id = ?
order by a.id

at
com.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:
204)
at
com.avaje.ebeaninternal.server.query.DefaultOrmQueryEngine.findMany(DefaultOrmQueryEngine.java:
89)
at
com.avaje.ebeaninternal.server.core.OrmQueryRequest.findList(OrmQueryRequest.java:
298)
at
com.avaje.ebeaninternal.server.core.DefaultServer.findList(DefaultServer.java:
1369)
at
com.avaje.ebeaninternal.server.core.DefaultBeanLoader.loadMany(DefaultBeanLoader.java:
162)
at
com.avaje.ebeaninternal.server.core.DefaultServer.loadMany(DefaultServer.java:
446)
at
com.avaje.ebeaninternal.server.loadcontext.DLoadManyContext.loadMany(DLoadManyContext.java:
142)
at
com.avaje.ebean.common.AbstractBeanCollection.lazyLoadCollection(AbstractBeanCollection.java:
152)
at com.avaje.ebean.common.BeanList.init(BeanList.java:116)
at com.avaje.ebean.common.BeanList.iterator(BeanList.java:288)
at idoneitaalloggiativa.testerror.TestError.main(TestError.java:38)
Caused by: org.h2.jdbc.JdbcSQLException: Column "AA.ID" not found; SQL
statement:
select a.id c0
, ap.id c1, ap.update_count c2, ap.abitazione_id c3, apc.id
c4, aa.id c5
from ABITAZIONE a
left outer join PRATICA ap on ap.abitazione_id = a.id
left outer join CONFORMITA apc on apc.pratica_id = ap.id
left outer join PARERE app on app.pratica_id = ap.id
where a.id = ?
order by a.id [42122-140 begin_of_the_skype_highlighting 42122-140 end_of_the_skype_highlighting]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:
327)
at org.h2.message.DbException.get(DbException.java:167)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:
127)
at org.h2.expression.Alias.optimize(Alias.java:47)
at org.h2.command.dml.Select.prepare(Select.java:738)
at org.h2.command.Parser.prepare(Parser.java:202)
at org.h2.command.Parser.prepareCommand(Parser.java:214)
at org.h2.engine.Session.prepareLocal(Session.java:434)
at org.h2.engine.Session.prepareCommand(Session.java:384)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:
1071)
at
org.h2.jdbc.JdbcPreparedStatement.(JdbcPreparedStatement.java:
71)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:
234)
at
com.avaje.ebeaninternal.server.lib.sql.PooledConnection.prepareStatement(PooledConnection.java:
426)
at
com.avaje.ebeaninternal.server.lib.sql.PooledConnection.prepareStatement(PooledConnection.java:
396)
at
com.avaje.ebeaninternal.server.query.CQuery.prepareBindExecuteQuery(CQuery.java:
396)
at
com.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:
162)
... 10 more

Ebean log is:

1-ago-2010 23.52.09 com.avaje.ebean.config.PropertyMapLoader load
GRAVE: ebean.properties not found
1-ago-2010 23.52.09
com.avaje.ebeaninternal.server.lib.sql.DataSourcePool initialise
INFO: DataSourcePool [certificazioni] autoCommit[false]
transIsolation[READ_COMMITTED] min[2] max[20]
1-ago-2010 23.52.10
com.avaje.ebeaninternal.server.core.DefaultServerFactory
setDatabasePlatform
INFO: DatabasePlatform name:certificazioni platform:h2
1-ago-2010 23.52.10
com.avaje.ebeaninternal.server.subclass.SubClassManager$1 run
INFO: SubClassFactory parent ClassLoader [sun.misc.Launcher
$AppClassLoader]
1-ago-2010 23.52.10
com.avaje.ebeaninternal.server.type.DefaultTypeManager
initialiseJodaTypes
INFO: Registering Joda data types
1-ago-2010 23.52.10
com.avaje.ebeaninternal.server.resource.ResourceManagerFactory
createFileSource
INFO: ResourceManager initialised: type[file] [C:\Documents and
Settings\claudio\Documenti\lavoro\workspace\idoneitaalloggiativa_old
\WebContent\WEB-INF]
1-ago-2010 23.52.10 com.avaje.ebeaninternal.server.deploy.DeployOrmXml
findAllOrmXml
INFO: No deployment xml (orm.xml etc) was loaded.
1-ago-2010 23.52.10
com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager logStatus
INFO: Entities enhanced[4] subclassed[0]
1-ago-2010 23.52.10
com.avaje.ebeaninternal.server.transaction.TransactionLogManager

INFO: Transaction logs in: logs
1-ago-2010 23.52.10
com.avaje.ebeaninternal.server.autofetch.AutoFetchManagerFactory
deserializeAutoFetch
INFO: AutoFetch deserialized from file [C:\Documents and Settings
\claudio\Documenti\lavoro\workspace\idoneitaalloggiativa_old\WebContent
\WEB-INF\.ebean.certificazioni.autofetch]
1-ago-2010 23.52.10
com.avaje.ebeaninternal.server.autofetch.DefaultAutoFetchManagerLogging
logToJavaLogger
INFO: AutoFetch queryTuning[true] profiling[true]
mode[DEFAULT_ONIFEMPTY] profiling rate[0.05] min[1] base[10]
1-ago-2010 23.52.10 com.avaje.ebean.Ebean
INFO: Ebean Version[2.6.1] Java Version[1.6.0_18]

If I leave only one OneToOne association in Pratica all works
properly.
Seems like an error generating the column alias of the OneToOne
properties of Pratica. Or perhaps my mapping is not correct.

 
Rob 18 Sep 09:48
Uploaded test case

Claudio uploaded a test case and I have reproduced this issue.

Definately a bug.

Rob 18 Sep 09:55
Fixed in HEAD

The issue was with DefaultDbSqlContext.
With multiple OneToOne relationship at the same level in the object graph the current prefix was being lost. Used a stack to push n pop the currentPrefix.

Fixed in HEAD.

woResponse

Upload a file