Scala and Hibernate examples, anyone?

518 views
Skip to first unread message

Michael Campbell

unread,
Dec 19, 2007, 7:13:43 AM12/19/07
to lif...@googlegroups.com
Does anyone have any examples of how you're stitching together scala
and hibernate? I'm looking for the most basic of basics; an example
scala domain class and how the hibernate mapping works with it, or
something at least at that level.

Thanks

David Pollak

unread,
Dec 19, 2007, 9:15:32 AM12/19/07
to lif...@googlegroups.com

On Dec 19, 2007, at 4:13 AM, Michael Campbell wrote:

>
> Does anyone have any examples of how you're stitching together scala
> and hibernate?

I'd like to see this too... especially in the wiki!

> I'm looking for the most basic of basics; an example
> scala domain class and how the hibernate mapping works with it, or
> something at least at that level.
>
> Thanks
>
> >

--
David Pollak
http://blog.lostlake.org

Viktor Klang

unread,
Dec 19, 2007, 9:42:48 AM12/19/07
to lif...@googlegroups.com
On 12/19/07, David Pollak <d...@athena.com> wrote:


On Dec 19, 2007, at 4:13 AM, Michael Campbell wrote:

>
> Does anyone have any examples of how you're stitching together scala
> and hibernate?

I'd like to see this too... especially in the wiki!
 
+1 for this! :)
 
(in my heart I hope for the Scala first class properties syntax to create a very sexy JPA integration)
 
Cheers,
Viktor

>   I'm looking for the most basic of basics; an example
> scala domain class and how the hibernate mapping works with it, or
> something at least at that level.
>
> Thanks
>
> >

--
David Pollak
http://blog.lostlake.org







        /lift/ committer (www.liftweb.net)
      SGS member (Scala Group Sweden)
  SEJUG member (Swedish Java User Group)
            Coffee drinker (Skånerost)
\_____________________________________/

edgarchan

unread,
Dec 19, 2007, 10:27:56 AM12/19/07
to liftweb
This is a simple and nice example:

http://technically.us/code/x/the-escape-hatch


On Dec 19, 6:13 am, "Michael Campbell" <michael.campb...@gmail.com>
wrote:

Jonas Bonér

unread,
Jan 8, 2008, 6:43:08 AM1/8/08
to liftweb
Here is a writeup:
http://www.artima.com/forums/flat.jsp?forum=276&thread=222229

On Dec 19 2007, 4:27 pm, edgarchan <edgarchancarri...@gmail.com>
wrote:
> This is a simple and nice example:
>
> http://technically.us/code/x/the-escape-hatch125
>
> On Dec 19, 6:13 am, "Michael Campbell" <michael.campb...126@gmail.com>

Steve Bendiola

unread,
Jan 16, 2008, 2:23:28 PM1/16/08
to liftweb
I found this mention of lift, but it makes it seem like you can't use
lift with hibernate.

http://jonasfagundes.com/blog/2008/01/grails-the-good-the-ugly-and-the-bad




On Dec 19 2007, 8:15 am, David Pollak <d...@athena.com> wrote:
> On Dec 19, 2007, at 4:13 AM, Michael Campbell wrote:
>
>
>
> > Does anyone have any examples of how you're stitching together scala
> > andhibernate?
>
> I'd like to see this too... especially in the wiki!
>
> > I'm looking for the most basic of basics; an example
> > scala domain class and how thehibernatemapping works with it, or

David Pollak

unread,
Jan 16, 2008, 2:40:38 PM1/16/08
to lif...@googlegroups.com
On 1/16/08, Steve Bendiola <steve.b...@gmail.com> wrote:

I found this mention of lift, but it makes it seem like you can't use
lift with hibernate.

http://jonasfagundes.com/blog/2008/01/grails-the-good-the-ugly-and-the-bad

THanks and I did a follow-up on his blog

On Dec 19 2007, 8:15 am, David Pollak < d...@athena.com> wrote:
> On Dec 19, 2007, at 4:13 AM, Michael Campbell wrote:
>
>
>
> > Does anyone have any examples of how you're stitching together scala
> > andhibernate?
>
> I'd like to see this too... especially in the wiki!
>
> >   I'm looking for the most basic of basics; an example
> > scala domain class and how thehibernatemapping works with it, or
> > something at least at that level.
>
> > Thanks
>
> --
> David Pollakhttp://blog.lostlake.org



--
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us

steve.yen

unread,
Jan 24, 2008, 10:27:00 PM1/24/08
to liftweb
Hi, I wrote up an single file example, here, of using Scala and
Hibernate...

http://scalabase.ning.com/profiles/blog/show?id=1808193%3ABlogPost%3A201

Cheers,
Steve

Fred Janon

unread,
Jan 24, 2008, 11:46:40 PM1/24/08
to lif...@googlegroups.com
Hi Steve,

I am Scala beginner, so sorry about the basic question...

I wonder why in withTxSession, you use:

    val r = f(s) // builds the function
    t.commit // commits the session
    r // executes the function

and not
 f(s) // executes the function with the session as a parameter
 t.commit // commits the session and save the obejcts created/updated in the function

Also, I thought that hibernate .jar included all the other jars but I can't check it at the moment.

Thanks

Fred

steve.yen

unread,
Jan 25, 2008, 12:56:22 AM1/25/08
to liftweb


On Jan 24, 8:46 pm, "Fred Janon" <fja...@gmail.com> wrote:
> Hi Steve,
>
> I am Scala beginner, so sorry about the basic question...
>
> I wonder why in withTxSession, you use:
>
> val r = f(s) // builds the function
> t.commit // commits the session
> r // executes the function
>
> and not
> f(s) // executes the function with the session as a parameter
> t.commit // commits the session and save the obejcts created/updated in the
> function
>

Hi Fred, I read it like this...

val r = f(s) // calls the function or callback f, saving the result
in variable r
t.commit // commits the transaction
r // returns the result that came from f

> Also, I thought that hibernate .jar included all the other jars but I can't
> check it at the moment.

You might be right. I didn't check that myself, but followed the
Hibernate tutorial and kept on adding jar files until the compiler
stopped complaining. Real teeth gritting trying to parse a durn
simple XML string into a org.w3c.dom.Document! dom4j? sax? meh.

Thanks,
Steve

>
> Thanks
>
> Fred

Daniel Green

unread,
Jan 26, 2008, 11:05:37 AM1/26/08
to lif...@googlegroups.com
My system for Hibernate+Scala was that persisted classes would have an
associated Actor DAO that they are tied to by a trait. This gives
each instance of an object a "save" and "delete" function. The Actor
DAOs were written so that they inherited (and could override) CRUD
operations. It was a pretty nifty system. There was also a bunch of
utility functions to aid searching and common operations. It was quite
simple to setup new database objects. I also had an ant file that
controlled an empty rails project so I could use migrations and model
generation to help manage the SQL side and (if I wanted to go that
far) generate hibernate mappings. I've gotten permission to release
this stuff into the wild if you guys think it's worth exploring.

jsm2prof

unread,
Jan 26, 2008, 12:55:10 PM1/26/08
to liftweb
Daniel Green,
I'm certainly eager to explore it. But docs with include plenty of
context help me to not be confused. The sample files you had
would be one of the most useful things for those who learn by
doing.
Johnnie

On Jan 26, 10:05 am, "Daniel Green" <october...@gmail.com> wrote:
> My system for Hibernate+Scala was that persisted classes would have an
> associated Actor DAO that they are tied to by a trait. This gives
> each instance of an object a "save" and "delete" function. The Actor
> DAOs were written so that they inherited (and could override) CRUD
> operations. It was a pretty nifty system. There was also a bunch of
> utility functions to aid searching and common operations. It was quite
> simple to setup new database objects. I also had an ant file that
> controlled an empty rails project so I could use migrations and model
> generation to help manage the SQL side and (if I wanted to go that
> far) generate hibernate mappings. I've gotten permission to release
> this stuff into the wild if you guys think it's worth exploring.
>

Fred Janon

unread,
Jan 27, 2008, 2:33:51 AM1/27/08
to lif...@googlegroups.com
Hi Daniel,
 
It would be great if you release it. I would love to look at it.
 
Thanks for sharing
 
Fred
 

Daniel Green

unread,
Jan 27, 2008, 6:49:23 PM1/27/08
to lif...@googlegroups.com
I rewrote allot of stuff and I haven't tested my rewrites yet (allot
of this was written in this email) but here's a general idea of the
stuff I was doing. Note that I assume that I can parametrize Class. I
did this here so I could unclutter my examples.

There was a base actor for all actors servicing a class:

abstract class ServiceActor[T](val servicedClass : Class[T]) extends Actor {
override def act {
loop {
react {
provide
}
}
}

def provide : PartialFunction[Any,Unit]
def services(f : PartialFunction[Any,Unit]) = f
}

so if i wanted to implement CRUD operations I'd first write a convenience trait:

trait CRUDServices[T <: POSO] {
val serviceActor : CRUDService[T]

def delete(poso : T) = serviceActor !? Delete(poso)
def get(id : int) : T = (serviceActor !? Get(id)).asInstanceOf[T]
def save(poso : T) : int = (serviceActor !? Save(poso)).asInstanceOf[int]
}

and then I'd implement that actor:

class CRUDService[T <: POSO](servicedClass : Class[T]) extends
ServiceActor[T](servicedClass) with CRUDServices {
override val serviceActor = this

override def provide = services {
case Save(poso) => reply {
transact((s, t) => s.save(poso))
poso.id
}
case Delete(poso) => reply(transact((s,t) => s.delete(poso)))
case Get(id) => reply {
transact { (s,t) =>
val poso = s.load(servicedClass, id).asInstanceOf[T]
Hibernate.initialize(poso)
poso.id = id
poso
}
}
}
}


and now for... THE END RESULT!

If I just wanted an instance of a generic crud service to manage a
class I could:

val service = new CRUDService(classOf[User])
service.start
service.delete(user)

or if I needed to batch a mass delete of users and then move on to do
other stuff:

users.foreach(service ! Delete(_))

Or if I wanted to get fancy and implement some non-crud operations or
override crud operations:

// This will need to be started before it can be used
object UserService extends CRUDService(classOf[User]) {
override def provide = services {
case GetFriends(id) => reply(/*...*/)
case Delete(poso) => reply(/* overrides generic delete */)
} orElse super.provide
}

and that's all you need to do stuff like

UserService.delete(user)
// or
val user = UserService.get(id)

This is a really cool system and you can wire it to do some very fun
stuff. With Terracotta integration it is even better. In my POSO
(plain old scala object) trait I have tie ins to crud operations so I
can do stuff like

user.delete
// and
user.save

I've also written a bunch of hibernate utilities for searching,
paginating, etc.

Viktor Klang

unread,
Jan 27, 2008, 7:17:49 PM1/27/08
to lif...@googlegroups.com
Would love to see your stuff man :)

-V

       /lift/ committer (www.liftweb.net)
     SGS member (Scala Group Sweden)
 SEJUG member (Swedish Java User Group)
\_____________________________________/
Reply all
Reply to author
Forward
0 new messages