referencedColumnName mapping problem

975 views
Skip to first unread message

rnentjes

unread,
Mar 29, 2010, 10:36:42 AM3/29/10
to Ebean ORM
Hi,

It looks like Ebean doesn't support referencedColumnName in ManyToOne
relations. It seems to require that the referenced column is an
primary key column. Is this correct?

For example, is it possible to map the company object in a user object
by matching them on a companyName field i.o. on a primary key.

Something like this in the user bean (entity?):

@ManyToOne
@JoinColumn(name="CompanyName", referencedColumnName="Name")
private Company company

And this in company:

@Id
private int id;

@Column(name="Name") // note, not a primary key field
private String name;

When we try something like this we get the following exception:

Caused by: javax.persistence.PersistenceException: Error with the Join
on [com.bla.model.contract.sale.TblSaleSched.prodSpecifications].
Could not find the local match for [ProdSpecCode] Perhaps an error in
a @JoinColumn
at
com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc.createImportedScalar(BeanPropertyAssoc.java:
393)
at
com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc.createImportedId(BeanPropertyAssoc.java:
350)
at
com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne.initialise(BeanPropertyAssocOne.java:
118)
at
com.avaje.ebeaninternal.server.deploy.BeanDescriptor.initialiseOther(BeanDescriptor.java:
650)
at
com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.initialiseAll(BeanDescriptorManager.java:
354)
at
com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.deploy(BeanDescriptorManager.java:
257)
at
com.avaje.ebeaninternal.server.core.InternalConfiguration.<init>(InternalConfiguration.java:
135)
at
com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:
189)
at
com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:
121)
at
com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:
67)
at
com.avaje.ebean.EbeanServerFactory.create(EbeanServerFactory.java:63)
at com.avaje.ebean.Ebean
$ServerManager.getWithCreate(Ebean.java:236)
at com.avaje.ebean.Ebean$ServerManager.<init>(Ebean.java:192)
at com.avaje.ebean.Ebean$ServerManager.<init>(Ebean.java:158)
at com.avaje.ebean.Ebean.<clinit>(Ebean.java:152)

Rob Bygrave

unread,
Mar 29, 2010, 5:33:38 PM3/29/10
to eb...@googlegroups.com

> It seems to require that the referenced column is an primary key column. Is this correct?

Yes.

What is the reason for trying to use company name rather than the primary key?
I'm guessing that you actually don't have foreign keys between these tables?



To unsubscribe from this group, send email to ebean+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

rnentjes

unread,
Mar 30, 2010, 4:01:32 AM3/30/10
to Ebean ORM
Hi Rob,

The reason for this is a legacy database, used by other applications
as well (we can't change it). There are hardly any foreign key
constraints and the implicit ones are not always on primary key. There
are also a lot of compound primary keys, complicating the matter.

Is there a rational for not supporting secondary key's? I would think
this is not an uncommon scenario.

Btw, I don't know what the JPA spec says about it, but the hibernate
implementation is fine with this mapping.

Actually this seems to be the only show stopper that prevents us from
moving to Ebean, after removing these mappings everything else still
seems to work fine.

Regards, Rien

On Mar 29, 11:33 pm, Rob Bygrave <robin.bygr...@gmail.com> wrote:
> > It seems to require that the referenced column is an primary key column.
>
> Is this correct?
>
> Yes.
>
> What is the reason for trying to use company name rather than the primary
> key?
> I'm guessing that you actually don't have foreign keys between these tables?
>

Rob Bygrave

unread,
Mar 30, 2010, 4:43:09 PM3/30/10
to eb...@googlegroups.com
> The reason for this is a legacy database

Cool. I just wanted to double check.



> Is there a rational for not supporting secondary key's?

We could support it. As you say Ebean is just searching the Id properties and instead it could search all the properties on the Ebean.

Generally speaking I'd expect an RDBMS to more efficiently join against the real primary key (smaller, less mutable, more likely in memory) relative to using an secondary index (like company name - bigger, more mutable, less likely to be in memory). I don't see any benefits in joining to a 'non primary key column'.

I'd hope it is uncommon but as you say, the design has already been baked.



> after removing these mappings everything else still seems to work fine.

Good to hear that.


Cheers, Rob.


To unsubscribe from this group, send email to ebean+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

Rob Bygrave

unread,
Mar 31, 2010, 12:52:45 AM3/31/10
to eb...@googlegroups.com
I logged this as BUG 263 : Add support for joining to Non - PK columns

http://www.avaje.org/bugdetail-263.html

I have added the code into HEAD and tested it out a little and it seems to work ok.  I have uploaded this as the 2.6.0-SNAPSHOT into the maven repository

... so, if you want to try it out and confirm that it works as expected that would be great.

The snapshot with this support added is:
http://www.avaje.org/archiva/repository/snapshots/org/avaje/ebean/2.6.0-SNAPSHOT/ebean-2.6.0-20100331.044526-1.jar


Cheers, Rob.

Rien

unread,
Mar 31, 2010, 5:24:56 AM3/31/10
to Ebean ORM
At a first glance this seems to work. I am going to create a more
extended proof of concept and will let you know when there are any
problems.

Thank you for the quick fix!

On Mar 31, 6:52 am, Rob Bygrave <robin.bygr...@gmail.com> wrote:
> I logged this as BUG 263 : Add support for joining to Non - PK columns
>
> http://www.avaje.org/bugdetail-263.html
>
> I have added the code into HEAD and tested it out a little and it seems to
> work ok.  I have uploaded this as the 2.6.0-SNAPSHOT into the maven
> repository
>
> ... so, if you want to try it out and confirm that it works as expected that
> would be great.
>

> The snapshot with this support added is:http://www.avaje.org/archiva/repository/snapshots/org/avaje/ebean/2.6...
>
> Cheers, Rob.


>
> On Wed, Mar 31, 2010 at 9:43 AM, Rob Bygrave <robin.bygr...@gmail.com>wrote:
>
> > > The reason for this is a legacy database
>
> > Cool. I just wanted to double check.
>
> > > Is there a rational for not supporting secondary key's?
>
> > We could support it. As you say Ebean is just searching the Id properties
> > and instead it could search all the properties on the Ebean.
>
> > Generally speaking I'd expect an RDBMS to more efficiently join against the
> > real primary key (smaller, less mutable, more likely in memory) relative to
> > using an secondary index (like company name - bigger, more mutable, less
> > likely to be in memory). I don't see any benefits in joining to a 'non
> > primary key column'.
>
> > I'd hope it is uncommon but as you say, the design has already been baked.
>
> > > after removing these mappings everything else still seems to work fine.
>
> > Good to hear that.
>
> > Cheers, Rob.
>

Message has been deleted

Charif Mauricio Nadir

unread,
Sep 12, 2013, 12:07:05 PM9/12/13
to eb...@googlegroups.com
Hi, is this feature in 2.8.1 ?

<groupId>org.avaje</groupId>
<artifactId>ebean</artifactId>
<version>2.8.1</version>

I am trying to do something similar. 

@Entity
@Table(name = "followers")
public class CDFollower
    @ManyToOne
    @JoinColumn(name = "email", referencedColumnName = "username")
    private CDAccount account;


The email is a varchar, also the username in account table, but I am getting this error

Caused by: javax.persistence.PersistenceException: Error loading on cl.home.data.entities.CDFollower.account
at com.avaje.ebeaninternal.server.query.SqlBeanLoad.load(SqlBeanLoad.java:132) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne.load(BeanPropertyAssocOne.java:618) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.query.SqlTreeNodeBean.load(SqlTreeNodeBean.java:276) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.query.CQuery.readRow(CQuery.java:545) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.query.CQuery.readBeanInternal(CQuery.java:579) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.query.CQuery.hasNextBean(CQuery.java:705) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.query.CQuery.readTheRows(CQuery.java:691) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.query.CQuery.readCollection(CQuery.java:658) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:199) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.query.DefaultOrmQueryEngine.findMany(DefaultOrmQueryEngine.java:96) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.core.OrmQueryRequest.findList(OrmQueryRequest.java:291) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.core.DefaultServer.findList(DefaultServer.java:1521) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.core.DefaultServer.findUnique(DefaultServer.java:1288) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery.findUnique(DefaultOrmQuery.java:922) [ebean-2.8.1.jar:]
at com.avaje.ebeaninternal.util.DefaultExpressionList.findUnique(DefaultExpressionList.java:193) [ebean-2.8.1.jar:]
...
...
...
Caused by: java.sql.SQLException: Invalid value for getInt() - 'em...@domain.tld'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2790)
at org.jboss.jca.adapters.jdbc.WrappedResultSet.getInt(WrappedResultSet.java:1052)
at com.avaje.ebeaninternal.server.type.RsetDataReader.getInt(RsetDataReader.java:138) [ebean-2.8.1.jar:]


It seems like ebean is trying to match email against the id (int field) of account instead do it by username.


Thanks in advance.
Reply all
Reply to author
Forward
0 new messages