How to implement Serializable interface in scala?

I have a scala class:

@Entity("users") class User(@Required val cid: String, val isAdmin: Boolean = false, @Required val dateJoined: Date = new Date() ) { @Id var id: ObjectId = _ @Reference val foos = new ArrayList[Foo] } 

If it were a Java class, I would just put java.io.Serializable, but this does not work in scala. Also, foos, as stated above, is private or public?

+6
source share
4 answers

scala 2.9.x also has an interface called Serializable, you can extend or mix it. up to 2.9.x @serializable choice is the only choice.

+2
source

How to use the @serializable scala object?

foos is public unless otherwise specified

+4
source

You can add the Serialized annotation to your Scala class (for example, in a JPA entity):

Since Serializable is a sign, you can mix it with a class even if your class already extends another class:

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

More about this link: https://www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch12s08.html

+2
source

An example of my Entity class (JPA) accepted 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
source

Source: https://habr.com/ru/post/891843/