2010-03-15 13 views
11

मैं हाइबरनेट एनोटेशन 3.4.0 का उपयोग कर स्कैला 2.8.0 में कुछ एनोटेटेड डोमेन कक्षाएं बना रहा हूं। यह ठीक काम कर रहा है, सिवाय इसके कि कुछ एनोटेशन हैं जो पैरामीटर के रूप में सरणी लेते हैं।मैं स्कैला 2.8 एनोटेशन में एक स्थिर सरणी कैसे निर्दिष्ट करूं?

[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) 

मैं कोशिश की है विभिन्न कोष्ठक, वर्ग /:

@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>) 
@OneToMany(mappedBy="passport_id", cascade={CascadeType.PERSIST}) 

... लेकिन दुर्भाग्य से मैं स्काला/जावा एनोटेशन की मेरी समझ के अंत पहुँच गए हैं। मदद की सराहना की है।

+5

क्या आपने 'कैस्केड = ऐरे (कैस्केड टाइप। PERSIST) 'का प्रयास किया था? –

+0

हां। इसने काम कर दिया। :-) धन्यवाद। –

उत्तर

14

रेक्स का समाधान क्यों काम करता है, यह समझाने के लिए मैं spec से कुछ स्निपेट जोड़ूंगा।

स्काला JVM पर के लिए, एनोटेशन कि उत्पन्न वर्ग के भीतर रख लिया जाएगा करने के लिए तर्क निरंतर भाव होना चाहिए:

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.

क्या निरंतर अभिव्यक्ति कर रहे हैं?

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).

तुम भी एक final val को तर्क refactor करने के लिए सक्षम होना चाहिए। हालांकि, यह Arrays के लिए काम नहीं प्रतीत होता है। मैं एक बग उठाऊंगा।

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

मेरे लिए काम नहीं करता है: @BeanProperty @Id @GeneratedValue (strategy = Array (GenerationType.IDENTITY)) var id: Long = _ त्रुटि देता है: सरणी निरंतर, प्रकार javax.persistence की अपेक्षित तर्क मिली। जनरेशन टाइप –

+1

एनोटेशन पैरामीटर 'रणनीति' केवल संकलक त्रुटि द्वारा सुझाए गए अनुसार एक मान स्वीकार करता है। http://download.oracle.com/javaee/5/api/javax/persistence/GeneratedValue.html 'strategy = GenerationType.IDENTITY' को चाल करना चाहिए। – retronym

+0

वे अभी भी सोच रहे हैं। http://stackoverflow.com/q/19060918/1296806 –

8

रेक्स केर से:

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

यह काम किया। धन्यवाद।

संबंधित मुद्दे