zenmeso / objectify-appengine

Automatically exported from code.google.com/p/objectify-appengine
MIT License
0 stars 0 forks source link

Wrong column names for fields inherited from Scala traits #108

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This issue is rather a request for enhancement than a bug report.

Consider the following sample Scala project:

package myproject.db.entity

import com.googlecode.objectify.annotation.Entity
import com.googlecode.objectify.annotation.Unindexed
import javax.persistence.Id
import javax.persistence.PrePersist

@Entity
class MyEntity extends HasVersion {
  @Id
  var id : java.lang.Long = null
}

@Unindexed
trait HasVersion {
  private var version: Long = 0

  final def getVersion() = version

  @PrePersist
  private def updateVersion(): Unit = {
    version += 1
  }
}

Scala compiler translates MyEntity to the following class file:

public class myproject.db.entity.MyEntity extends java.lang.Object implements 
myproject.db.entity.HasVersion,scala.ScalaObject{
    private java.lang.Long id;
    private long myproject$db$entity$HasVersion$$version;
    public final long myproject$db$entity$HasVersion$$version();
    public final void myproject$db$entity$HasVersion$$version_$eq(long);
    public final long getVersion();
    public java.lang.Long id();
    public void id_$eq(java.lang.Long);
    public myproject.db.entity.MyEntity();
}

As you can see, field 'version' is translated to 
'myproject$db$entity$HasVersion$$version' (that's because scala traits cannot 
be represented seamlessly in java).

I expect that the column name remain 'version' in the datastore despite the 
field is translated to 'myproject$db$entity$HasVersion$$version'. In fact, the 
ugly 'myproject$db$entity$HasVersion$$version' is used as the column name.

As a workaround I could define 'version' as an abstract field in HasVersion.
The disadvantage of this solution is the necessity to re-define the field in 
every class extending HasVersion.

To fix the issue I suggest implementing one of the following:

1. Add support for JPA's Column annotation to specify the column name:

@Column(name='version')
private var version: Long = 0

2. Or (even better) simply discard everything prior to $$ in the field names 
and use the remaining string as the column name. So the field named 
'myproject$db$entity$HasVersion$$version' will be seen as 'version' in the 
datastore.

What version of the product are you using? Objectify 3.0.

Original issue reported on code.google.com by v.kary...@gmail.com on 2 Oct 2011 at 8:21

GoogleCodeExporter commented 9 years ago
Ouch.  Yeah, we can do something about this.  #2 sounds pretty good.

Original comment by lhori...@gmail.com on 4 Oct 2011 at 3:01

GoogleCodeExporter commented 9 years ago
> #2 sounds pretty good.

Indeed, this is a simpler solution.

My only doubt though is that the scala-to-java translation mapping is not 
documented: http://www.scala-lang.org/node/265

Original comment by v.kary...@gmail.com on 4 Oct 2011 at 6:11

GoogleCodeExporter commented 9 years ago
Cleaning out old issues - is this still a problem?

Original comment by lhori...@gmail.com on 14 Apr 2014 at 12:28

GoogleCodeExporter commented 9 years ago
I'm not using Objectify in any Scala project any longer,  so this is not an 
issue for me :)

Original comment by v.kary...@gmail.com on 14 Apr 2014 at 3:16