开发者

How to implement interface Serializable in scala?

开发者 https://www.devze.com 2023-03-17 05:19 出处:网络
I have scala class like: @Entity(\"users\") class User(@Required val cid: String, val isAdmin: Boolean = false, @Required val dateJoined: Date = n开发者_Go百科ew Date() ) {

I have scala class like:

@Entity("users")
class User(@Required val cid: String, val isAdmin: Boolean = false, @Required val dateJoined: Date = n开发者_Go百科ew Date() ) {
  @Id var id: ObjectId = _



    @Reference
  val foos = new ArrayList[Foo]

    }

If it was a Java class I would simply put implements java.io.Serializable but this does not work in scala. Also is foos as declared above is private or public?


How do I use a @serializable scala object?

foos is public unless marked otherwise


scala 2.9.x also have an interface named Serializable, you may extends or mixin this. before 2.9.x the @serializable is the only choice.


You can add Serialized annotation on your Scala Class (at JPA Entity for example):

Because Serializable is a trait, you can mix it into a class, even if your class already extends another class:

@SerialVersionUID(114L)
class Employee extends Person with Serializable ...

Se more details at this link: https://www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch12s08.html


An example of my Entity (JPA) class writed in scala, using Serialized properties:

import javax.persistence._

import scala.beans.BeanProperty
import java.util.Date

@SerialVersionUID(1234110L)
@Entity
@Table(name = "sport_token")
class Token() extends Serializable {

  @Id
  @SequenceGenerator(name="SPORT_TOKEN_SEQ",catalog="ESPORTES" , sequenceName="SPORT_TOKEN_SEQ", allocationSize=1)
  @GeneratedValue(strategy=GenerationType.SEQUENCE , generator="SPORT_TOKEN_SEQ")
  @BeanProperty
  var id: Int = _

  @BeanProperty
  @Column(name="token")
  var token: String = _

  @BeanProperty
  @Column(name="active")
  var active: Int = _

}
0

精彩评论

暂无评论...
验证码 换一张
取 消