sorm
A functional boilerplate-free Scala ORM
Introduction - SORM a functional boilerplate-free scala orm
I'm using playframework 2.2.1 with scala 2.10 and SORM 0.3.10 for mysql db.
When I'm trying to save instance of simple case class:
case class User(email: String, password: String, token: String, verified: Boolean = false, atoken: UserAuthToken) {
def save = Db.save[User](this)
}
I'm gettin this error:
sorm.core.SormException: Attempt to refer to an unpersisted entity: UserAuthToken(7779235c1fd045f39ced7674a45baaa2,1387039847)
What I'm doing wrong? UserAuthToken
is quite simple too:
case class UserAuthToken(token: String = UUID.randomUUID().toString.replace("-",""), expire: Int = (Calendar.getInstance().getTimeInMillis/1000).toInt + 60*60*365)
Both classes are registered as entities in Db object.
Source: (StackOverflow)
Is it possible to fetch items by plain SQL query instead of building query by DSL using SORM?
For example is there an API for making something like
val metallica = Db.query[Artist].fromString("SELECT * FROM artist WHERE name = ?", "Metallica").fetchOne() // Option[Artist]
instead of
val metallica = Db.query[Artist].whereEqual("name", "Metallica").fetchOne() // Option[Artist]
Source: (StackOverflow)
I was looking for a lightweight Hibernate alternative for a small Scala project and SORM looks very promising. Currently I use SQLite, but according to the official website it is not supported. So my question is are there any plans to add SQLite support in SORM?
Source: (StackOverflow)
I see that Sorm already supports org.joda.time.DateTime
. Is there a possibility to add support for other types?
For example, my case class has a java.nio.charset.Charset
or Locale
field, which I would like to convert to a string. Suppose I have functions to accomplish the conversion from the custom type to/from a SQL type, how can I tell Sorm to use it?
Source: (StackOverflow)
I'm very interested in SORM, but when I try to use it I bump into problem. Suppose I have two entities:
case class User(login: String, firstName: String, lastName: String)
case class UserSite(userId: Int, name: String, url: String)
How can I declare foreign key relation UserSite.userId -> User.id? I see class ForeignKey, but there are no any example of using it.
Thank you.
Source: (StackOverflow)
I want to iterate over all records of a specific table in sorm, but I want to do it in a way that it is memory efficient.
The code that I use today is:
Db.query[Items].whereEqual("title", someTitle).fetch.foreach { webCount =>
//do something
}
The problem is that this code first loads all records, before going into each item. Is there any way to stream the records?
Source: (StackOverflow)
I am trying to share an in-memory H2 Database I created using SORM framework in my Play Framework application. Below is the code for my database code
object DB extends Instance(entities = Seq(Entity[Person]()), url ="jdbc:h2:mem:db1"){}
The solution stated at H2 Database site is to start a TCP Server. In Java applications I am able to share the database using the following code
org.h2.tools.Server server = org.h2.tools.Server.createTcpServer();
server.start();
Connection conn = DriverManager.getConnection("jdbc:h2:mem:db1");
How can I start a tcp server in my Play application when it starts or when it is running?
Source: (StackOverflow)
Is it possible to filter the rows which string-columns contains special substring? Expressions like .whereContains("name", "Pavel")
throws exception: Exception: Filter "Contains" is not supported for mapping "String"
. Of course, I can easily use scala filter but DB should do it better, isn't it?
Source: (StackOverflow)
I use H2 database in embedded mode with SORM.
If the database is busy then SORM just continue to wait. There are no exception, nothing happens. This is misleading. :(
So how i can set the db connection timeout?
Source: (StackOverflow)
I see that into log during SORM 0.3.8 initialization with 2 entities:
[] ?_$5 setInst Int
[] ?_$5 setInst Int
What is this?
Source: (StackOverflow)
I wonder if anyone can share some best practices for using SORM, with reference to using in actor systems and with automated testing (unit/functional).
How do you:
- drive your DB/Instance singleton from externalized configuration that's constructed at runtime (eg. typesafe's application.conf)
- do dependency injection (either constructor or cake pattern or ?) to use the same code with production/test DBs .. or do you use another strategy altogether?
It seems that when I don't declare my "Instance" as a singleton (object extends Instance) but rather create a new Instance for injecting into my actors for testing, I encounter strange stack traces using SORM. I used scala 2.10.2/3, SORM 0.38/9 with the same results. Here's my (only) entity:
object TapJoy {
private val SECRET_KEY = "aaa"
case class AddCreditsRequest(tapJoyId: String, verifier: String, currency: Int, snuid: Long) {
val created = new DateTime()
<some methods omitted>
}
}
stack traces:
java.lang.ClassCastException: scala.reflect.internal.Types$TypeRef$$anon$3 cannot be cast to scala.reflect.internal.Symbols$Symbol
at scala.reflect.internal.pickling.UnPickler$Scan.readSymbolRef(UnPickler.scala:788)
at scala.reflect.internal.pickling.UnPickler$Scan.readType(UnPickler.scala:355)
at scala.reflect.internal.pickling.UnPickler$Scan$LazyTypeRef$$anonfun$34.apply(UnPickler.scala:855)
at scala.reflect.internal.pickling.UnPickler$Scan$LazyTypeRef$$anonfun$34.apply(UnPickler.scala:855)
at scala.reflect.internal.pickling.UnPickler$Scan.at(UnPickler.scala:171)
at scala.reflect.internal.pickling.UnPickler$Scan$LazyTypeRef.complete(UnPickler.scala:855)
at scala.reflect.internal.Symbols$Symbol.info(Symbols.scala:1229)
at scala.reflect.internal.Types$TypeRef.thisInfo(Types.scala:2399)
at scala.reflect.internal.Types$TypeRef.baseClasses(Types.scala:2404)
at scala.reflect.internal.Types$Type.findMembers(Types.scala:1093)
at scala.reflect.internal.Types$Type.membersBasedOnFlags(Types.scala:718)
at scala.reflect.internal.Types$Type.members(Types.scala:661)
at scala.reflect.internal.Types$Type.members(Types.scala:343)
at sorm.reflection.ScalaApi$TypeApi.members(ScalaApi.scala:11)
at sorm.reflection.ScalaApi$TypeApi.properties(ScalaApi.scala:13)
at sorm.reflection.Reflection.properties(Reflection.scala:31)
at sorm.core.Initialization$.errors(Initialization.scala:29)
at sorm.Instance$Initialization$$anonfun$2.apply(Instance.scala:212)
at sorm.Instance$Initialization$$anonfun$2.apply(Instance.scala:212)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.immutable.Set$Set1.foreach(Set.scala:74)
at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
at sorm.Instance$Initialization.<init>(Instance.scala:212)
at sorm.Instance.<init>(Instance.scala:29)
or
scala.reflect.internal.Symbols$CyclicReference: illegal cyclic reference involving method productPrefix
at scala.reflect.internal.Symbols$Symbol$$anonfun$info$3.apply(Symbols.scala:1218)
at scala.reflect.internal.Symbols$Symbol$$anonfun$info$3.apply(Symbols.scala:1216)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.reflect.internal.Symbols$Symbol.lock(Symbols.scala:482)
at scala.reflect.internal.Symbols$Symbol.info(Symbols.scala:1216)
at scala.reflect.internal.Symbols$Symbol.tpe(Symbols.scala:1200)
at scala.reflect.internal.Symbols$Symbol.tpeHK(Symbols.scala:1201)
at scala.reflect.internal.Types$Type.computeMemberType(Types.scala:784)
at scala.reflect.internal.Symbols$MethodSymbol.typeAsMemberOf(Symbols.scala:2646)
at scala.reflect.internal.Types$Type.memberType(Types.scala:779)
at scala.reflect.internal.Types$Type.findMembers(Types.scala:1117)
at scala.reflect.internal.Types$Type.membersBasedOnFlags(Types.scala:718)
at scala.reflect.internal.Types$Type.members(Types.scala:661)
at scala.reflect.internal.Types$Type.members(Types.scala:343)
at sorm.reflection.ScalaApi$TypeApi.members(ScalaApi.scala:11)
at sorm.reflection.ScalaApi$TypeApi.properties(ScalaApi.scala:13)
at sorm.reflection.Reflection.properties(Reflection.scala:31)
at sorm.core.Initialization$.errors(Initialization.scala:29)
at sorm.Instance$Initialization$$anonfun$2.apply(Instance.scala:212)
at sorm.Instance$Initialization$$anonfun$2.apply(Instance.scala:212)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:251)
at scala.collection.immutable.Set$Set1.foreach(Set.scala:74)
at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
at sorm.Instance$Initialization.<init>(Instance.scala:212)
at sorm.Instance.<init>(Instance.scala:29)
Source: (StackOverflow)
I am just trying to learn SORM and am playing with what I think is some simple sample code. To whit:
case class Book(var author:String, var title:String);
object Db extends Instance(
entities = Set(Entity[Book]()),
url = "jdbc:h2:mem:test",
user = "",
password = "",
initMode = InitMode.Create
)
And then:
val b : Book with Persisted = Db.save( Book("foo","bar") )
When attempting to compile this, I get:
[error] /Users/rjf/IdeaProjects/PlayTest/app/controllers/Application.scala:22: type mismatch;
[error] found : models.Book
[error] required: sorm.Persisted with models.Book
[error] val b : Book with Persisted = Db.save( Book("foo","bar") )
[error] ^
If I change the Book declaration to:
case class Book(var author:String, var title:String) extends Persisted;
I then get:
[error] /Users/rjf/IdeaProjects/PlayTest/app/models/Book.scala:17: class Book needs to be abstract, since:
[error] it has 2 unimplemented members.
[error] /** As seen from class Book, the missing signatures are as follows.
[error] * For convenience, these are usable as stub implementations.
[error] */
[error] def id: Long = ???
[error] def mixoutPersisted[T]: (Long, T) = ???
[error] case class Book(var author:String, var title:String) extends Persisted;
[error] ^
Apologies for the newbie question. No doubt that the fact I'm learning Scala at the same time is a contributing factor.
Source: (StackOverflow)
I have an sbt project with these dependencies:
libraryDependencies ++= Seq(
"org.scalatra" %% "scalatra" % ScalatraVersion,
"org.scalatra" %% "scalatra-scalate" % ScalatraVersion,
"org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
"ch.qos.logback" % "logback-classic" % "1.1.2" % "runtime",
"org.eclipse.jetty" % "jetty-webapp" % "9.1.5.v20140505" % "container",
"org.eclipse.jetty" % "jetty-plus" % "9.1.5.v20140505" % "container",
"javax.servlet" % "javax.servlet-api" % "3.1.0",
"org.sorm-framework" % "sorm" % "0.3.18",
"com.h2database" % "h2" % "1.4.187",
"org.fusesource.scalamd" % "scalamd_2.10" % "1.6"
)
On one machine, I can run ./sbt without issue; on the other, I get
[error] Modules were resolved with conflicting cross-version suffixes in {file:/C:/dev/scalaspace/game-tracker/}game-tracker:
[error] org.scala-lang.modules:scala-xml _2.11, _2.12.0-M1
[error] org.scala-lang.modules:scala-parser-combinators _2.11, _2.12.0-M1
I've already isolated the problem and the fix. Sorm depends on the 2.12.0-M1 compiler:
[info] +-org.sorm-framework:sorm:0.3.18 [S]
[info] +-com.github.nikita-volkov:embrace:0.1.4 [S]
[info] | +-org.scala-lang:scala-compiler:2.12.0-M1 [S]
[info] | +-org.scala-lang.modules:scala-parser-combinators_2.12.0-M1:1.0.4 [S]
[info] | +-org.scala-lang.modules:scala-xml_2.12.0-M1:1.0.4 [S]
[info] | +-org.scala-lang:scala-reflect:2.11.0 [S] (evicted by: 2.11.6)
[info] | +-org.scala-lang:scala-reflect:2.11.6 [S]
[info] | +-org.scala-lang:scala-reflect:2.12.0-M1 (evicted by: 2.11.0)
I can make the project build in both locations by adding exclude("org.scala-lang","scala-compiler")
to the sorm dependency. But why is the behavior inconsistent? Both environments are using the same version of sbt (0.13.8) and scala (2.11.6). What's different?
Source: (StackOverflow)
How add enums in SORM?
I have this enumeration:
object RoutineType extends Enumeration {
val TimeRoutine, SetRoutine = Value
}
and in DB object I add entity:
entities = Set() + Entity[RoutineType.Value]()
but when I load everything I get this exception:
Caused by: sorm.core.SormException: Unsupported type: Enumeration
Why? Documentation tells enums are supported
Source: (StackOverflow)
I have created simple sbt project from tutorial:
build.sbt:
lazy val sorm_test = (project in file(".")).
settings(
name := "SORM_TEST",
scalaVersion := "2.11.7",
libraryDependencies ++= Seq(
"org.sorm-framework" % "sorm" % "0.3.18",
"com.h2database" % "h2" % "1.4.188"
)
)
test.Main.scala:
package test
case class Artist(
names : Map[Locale, Seq[String]],
genres : Set[Genre]
)
case class Genre(
names : Map[Locale, Seq[String]]
)
case class Locale(
code : String
)
import sorm._
object Db extends Instance(
entities = Set(
Entity[Artist](),
Entity[Genre](),
Entity[Locale](unique = Set() + Seq("code"))
),
url = "jdbc:h2:mem:test",
user = "",
password = "",
initMode = InitMode.Create
)
object Main extends App {
// init
Db.##
}
When i run this project in intellij i have such exceptions :
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.parse(ToolBoxFactory.scala:414)
at sorm.persisted.PersistedClass$.createClass(PersistedClass.scala:107)
at sorm.persisted.PersistedClass$$anon$1$$anonfun$resolve$1.apply(PersistedClass.scala:125)
at sorm.persisted.PersistedClass$$anon$1$$anonfun$resolve$1.apply(PersistedClass.scala:125)
at scala.collection.mutable.MapLike$class.getOrElseUpdate(MapLike.scala:194)
at scala.collection.mutable.AbstractMap.getOrElseUpdate(Map.scala:80)
at sorm.persisted.PersistedClass$$anon$1.resolve(PersistedClass.scala:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sorm.persisted.PersistedClass$.apply(PersistedClass.scala:129)
at sorm.Instance$Initialization$$anonfun$9$$anonfun$apply$16.apply(Instance.scala:239)
at sorm.Instance$Initialization$$anonfun$9$$anonfun$apply$16.apply(Instance.scala:239)
at embrace.package$EmbraceAny$.$$extension(package.scala:6)
at sorm.Instance$Initialization$$anonfun$9.apply(Instance.scala:239)
at sorm.Instance$Initialization$$anonfun$9.apply(Instance.scala:239)
at scala.collection.immutable.Set$Set3.foreach(Set.scala:145)
at sorm.Instance$Initialization.<init>(Instance.scala:239)
at sorm.Instance.<init>(Instance.scala:38)
at test.Db$.<init>(Main.scala:15)
at test.Db$.<clinit>(Main.scala)
at test.Main$.delayedEndpoint$test$Main$1(Main.scala:29)
at test.Main$delayedInit$body.apply(Main.scala:27)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at test.Main$.main(Main.scala:27)
at test.Main.main(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1
... 38 more
Caused by: java.lang.ClassNotFoundException: scala.runtime.java8.JFunction1
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 38 more
It's ok when i use sbt run
. This exception is thrown also when i integrate SORM with Play framework. How can i solve this problem ?
Source: (StackOverflow)