2010-03-15 2 views
11

Ich habe einige annotierte Domain-Klassen in Scala 2.8.0 mit Hibernate Annotations 3.4.0 erstellt. Es funktioniert gut, außer dass es bestimmte Annotationen gibt, die ein Array als Parameter verwenden. Zum Beispiel, hier ist eine Java Annotations, die ich in Scala ausdrücken will:Wie lege ich ein statisches Array in einer Scala 2.8 Annotation fest?

@OneToMany(mappedBy="passport_id", cascade=CascadeType.PERSIST) 

erfordert jedoch die Anmerkung ein Array/Set als Eingabe:

[ERROR] .../Passport.scala:50: error: type mismatch; 
[INFO] found : javax.persistence.CascadeType(value PERSIST) 
[INFO] required: Array[javax.persistence.CascadeType] 
[INFO]  @OneToMany(mappedBy="passport_id", cascade=CascadeType.PERSIST) 

Ich habe versucht, verschiedene Klammern, quadratisch/Winkel/geschweiften Klammern, und so weiter:

@OneToMany(mappedBy="passport_id", cascade=(CascadeType.PERSIST)) 
@OneToMany(mappedBy="passport_id", cascade=[CascadeType.PERSIST]) 
@OneToMany(mappedBy="passport_id", cascade=<CascadeType.PERSIST>) 
@OneToMany(mappedBy="passport_id", cascade={CascadeType.PERSIST}) 

... aber leider habe ich das Ende meines Verständnisses von Scala/Java-Annotationen erreicht. Hilfe wird geschätzt.

+5

Haben Sie 'cascade = Array (CascadeType.PERSIST) 'versucht? –

+0

Ja. Es funktionierte. :-) Vielen Dank. –

Antwort

14

Ich füge ein paar Schnipsel von der spec hinzu, um zu erklären, warum die Lösung von Rex funktioniert.

Für Scala auf der JVM, Argumente zu Anmerkungen, die innerhalb der generierten Klasse beibehalten werden müssen konstante Ausdrücke sein:

Instances of an annotation class inheriting from trait scala.ClassfileAnnotation will be stored in the generated class files. ... Additionally, on both Java and .NET, all constructor arguments must be constant expressions.

Was sind konstante Ausdrücke?

6.24 Constant Expressions Constant expressions are expressions that the Scala compiler can evaluate to a constant. The definition of “constant expression” depends on the platform, but they include at least the expressions of the following forms:

  • A literal of a value class, such as an integer
  • A string literal
  • A class constructed with Predef.classOf (§12.4)
  • An element of an enumeration from the underlying platform
  • A literal array, of the form Array(c1, . . . , cn), where all of the ci ’s are themselves constant expressions
  • An identifier defined by a constant value definition (§4.1).

Sie sollten auch das Argument einer final val Refactoring können. Dies scheint jedoch nicht für Arrays zu funktionieren. Ich werde einen Fehler auslösen.

class T(value: Any) extends ClassfileAnnotation 

object Holder { 
    final val as = Array(1, 2, 3) 
    final val a = 1 
} 

@T(Holder.a) 
@T(Holder.as) // annot.scala:9: error: annotation argument needs to be a constant; found: Holder.as 
class Target 
+0

Funktioniert nicht für mich: @BeanProperty @Id @GeneratedValue (Strategie = Array (GenerationType.IDENTITY)) var ID: Lang = _ gibt Fehler: gefundene Arraykonstante, erwartetes Argument vom Typ javax.persistence.GenerationType –

+1

Der Annotationsparameter "Strategie" akzeptiert nur einen einzelnen Wert, wie vom Compilerfehler vorgeschlagen. http://download.oracle.com/javaee/5/api/javax/persistence/GeneratedValue.html "strategie = GenerationType.IDENTITY" sollte den Trick machen. – retronym

+0

Sie fragen sich immer noch. http://stackoverflow.com/q/19060918/1296806 –

8

Von Rex Kerr:

@OneToMany(mappedBy="passport_id", cascade=Array(CascadeType.PERSIST)) 

Das funktionierte. Vielen Dank.

Verwandte Themen