- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Running into problems using @OneToMany in Scala
Fri, 2009-04-03, 20:24
Hi everyone,
I'm trying to rewrite a java entity in Scala. However, the 2 arguement
OneToMany annotation did not work(I got a wrong number of arguments error).
I then found this thread:
http://www.nabble.com/-scala--Annotations,-Generics-and-JPA-td20953349.html
and tried to follow it but it still doesnt compile. Can someone please help
me?
The Scala class looks like this:
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence._
import scala.reflect._
@Entity class Question {
@Id @GeneratedValue @BeanProperty var id:Int = 0;
@BeanProperty var text:String = null;
@OneToMany(val mappedBy = "sampleElement",val targetEntity =
classOf[Contact], val fetch=FetchType.EAGER, val
cascade=Array(CascadeType.ALL)) @BeanProperty var choices = new
ArrayList[Choice]()
@OneToOne @BeanProperty var answer:Choice = null
}
The coresponding Java class is:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
@Entity
public class Question implements Serializable {
@Id
@GeneratedValue
private int id;
private String text;
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private Collection choices = new ArrayList();
@OneToOne
private Choice answer;
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Choice getAnswer() {
return answer;
}
public void setAnswer(Choice answer) {
this.answer = answer;
}
public Collection getChoices() {
return choices;
}
public void setChoices(Collection choices) {
this.choices = choices;
}
}
Fri, 2009-04-03, 20:57
#2
Re: Running into problems using @OneToMany in Scala
fantastic_ray wrote:
> OneToMany annotation did not work
It's @OneToMany{...}, not @OneToMany(...)
- Florian
Second attempt:
package edu.sjsu.simplequiz.entity
import java.util.Collection
import javax.persistence._
import scala.reflect._
@Entity class Question {
@Id @GeneratedValue @BeanProperty var id:Int = 0;
@BeanProperty var text:String = null;
@OneToMany{ val mappedBy="folder", val targetEntity=classOf[Choice] }
@BeanProperty var choices: java.util.ArrayList[Choice] = new
java.util.ArrayList[Choice]();
@OneToOne @BeanProperty var answer:Choice = null;
}