This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Running into problems using @OneToMany in Scala

2 replies
fantastic_ray
Joined: 2009-04-03,
User offline. Last seen 42 years 45 weeks ago.

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;
}
}

fantastic_ray
Joined: 2009-04-03,
User offline. Last seen 42 years 45 weeks ago.
Re: Running into problems using @OneToMany in Scala

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;
}

Florian Hars
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Running into problems using @OneToMany in Scala

fantastic_ray wrote:
> OneToMany annotation did not work

It's @OneToMany{...}, not @OneToMany(...)

- Florian

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland