annotations - Using scala constants in constant expressions -
i have constants, made of other, smaller constants. example
object myconstants { final val tablename = "table_name"; final val fieldname = "field_name"; final val indexname = tablename + "_" + fieldname + "_ix"; // 1 not want constant }
i want these true constants because use them in annotations.
how make work? (on scala 2.11)
what want
interface myconstants { string tablename = "table_name"; string fieldname = "field_name"; string indexname = tablename + "_" + fieldname + "_ix"; }
but in scala. scalac not pick constants usage in annotations if compiles them java class/interface (see si-5333) decided put them in scala object. works literals, , expressions literals, not expressions other constants.
with code:
import javax.persistence.entity import javax.persistence.table import org.hibernate.annotations.index object myconstants { final val tablename = "table_name"; final val fieldname = "field_name"; final val indexname = tablename + "_" + fieldname + "_ix"; } @entity @table(name = myconstants.tablename) @org.hibernate.annotations.table( appliesto = myconstants.tablename, indexes = array(new index(name = myconstants.indexname, columnnames = array(myconstants.fieldname)))) class myentity { }
i following error on line indexes = ...
annotation argument needs constant; found: myconstants.indexname
edit: after fiddling around few build configurations, think scala-ide specific issue. code indeed compile alright when build project gradle or sbt. use build tool actual projects, in end it's having few incomprehensible markers in ide - annoying, has little functionality.
i used constants in jpa scala. code compiles, , used it:
freedays.scala
@entity @table(name = "free_days") @namedqueries( array( new namedquery(name = jpaqueries.is_free_days, query = "select f freedays f f.dateoffreeday = :" + jpaqueries.date) ) ) class freedays { def this(id: int, name: string, dateoffreeday: date) = { this() this.id = id this.name = name this.dateoffreeday = dateoffreeday } @id @generatedvalue var id: long = _ var name: string = _ @column(name = "date_of_free_day") @temporal(temporaltype.date) var dateoffreeday: date = _ }
jpaqueries.scala
object jpaqueries extends jpaqueries sealed trait jpaqueries { final val is_free_days = "is_free_days" final val date = "date" }
Comments
Post a Comment