Mapping a table to a scala class example

33 views
Skip to first unread message

Kaiyin Zhong

unread,
Mar 20, 2016, 7:33:24 PM3/20/16
to Lift
I am following an example from the lift application development cookbook and the project is here: https://github.com/kindlychung/liftmapper 

I created a data source like this:

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="dsliftbook" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg>jdbc/dsliftbook</Arg>
        <Arg>
            <New class="org.postgresql.ds.PGSimpleDataSource">
                <Set name="User">kaiyin</Set>
                <Set name="Password">________</Set>
                <Set name="DatabaseName">liftbook</Set>
                <Set name="ServerName">localhost</Set>
                <Set name="PortNumber">5432</Set>
            </New>
        </Arg>
    </New>
</Configure>


The mapper is like this:

class Contact extends LongKeyedMapper[Contact] with IdPK {
  def getSingleton = Contact

  object name extends MappedString(this, 100)
}

object Contact extends Contact with LongKeyedMetaMapper[Contact] {
  override def dbTableName = "contacts"
}

And the new entries are created like this:


class ContactsObj {
  def prepareContacts_!() {
    Contact.findAll().map(_.delete_!)
    val contactsNames = "John" :: "Joe" :: "Lisa" :: Nil
    contactsNames.foreach(Contact.create.name(_).save())

  }

  def list = {
    println("list contacts")
    prepareContacts_!()

    "li *" #> Contact.findAll().map {
      c => c.name.get
    }
  }
}


Call the list method from the front end:


<div data-list="ContactsObj.list">
    <ul>
        <li></li>
    </ul>
</div>


The strange thing is that the console didn't even print out "list contacts". Any suggestions?




Antonio Salazar Cardozo

unread,
Mar 21, 2016, 12:25:09 AM3/21/16
to Lift
Hey there,
In trying to boot your app I saw this:

java.lang.NullPointerException: Looking for Connection Identifier ConnectionIdentifier(jdbc/dsliftbook) but failed to find either a JNDI data source with the name jdbc/dsliftbook or a lift connection manager with the correct name

Not a huge mapper person, but are you seeing this error as well? That
could break things.
Thanks,
Antonio

Kaiyin Zhong (Victor Chung)

unread,
Mar 21, 2016, 3:41:48 AM3/21/16
to lif...@googlegroups.com

You need to have postgresql running, with user name, password, database name set up correctly in the XML file.

Best regards,

Kaiyin ZHONG
------------------
FMB, Erasmus MC
kaiyin.co.vu
k.z...@erasmusmc.nl
kindl...@gmail.com
   

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

---
You received this message because you are subscribed to a topic in the Google Groups "Lift" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/liftweb/C3LRtkKMPC8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kaiyin Zhong (Victor Chung)

unread,
Mar 21, 2016, 4:10:22 AM3/21/16
to lif...@googlegroups.com
And by the way, I just checked that the database is working as expected, except for that the table is empty:

psql -U kaiyin -W liftbook
Password for user kaiyin:
psql (9.5.1)
Type "help" for help.

liftbook=# \d+ contacts
                                                     Table "public.contacts"
 Column |          Type          |                       Modifiers                       | Storage  | Stats target | Description
--------+------------------------+-------------------------------------------------------+----------+--------------+-------------
 name   | character varying(100) |                                                       | extended |              |
 id     | bigint                 | not null default nextval('contacts_id_seq'::regclass) | plain    |              |
Indexes:
    "contacts_pk" PRIMARY KEY, btree (id)

liftbook=# select * from contacts;
 name | id
------+----
(0 rows)


Best regards,

Kaiyin ZHONG
________________________________
FMB, Erasmus MC
http://kspace.co.vu

Peter Petersson

unread,
Mar 21, 2016, 5:58:56 AM3/21/16
to lif...@googlegroups.com
Hi
Following up on Antonio's findings.

In your code listing you are not showing the database setup things that you should have in your bootstrap.liftweb.Boot boot method.

Maybe something like
DefaultConnectionIdentifier.jndiName = "jdbc/dsliftbook"
would help.

I have personally not used jndi naming with lift but this seems to be the way to do it, at least It may give you some hints on how to proceed.

[1] https://books.google.se/books?id=cOvcIuacmmcC&pg=PT249&lpg=PT249&dq=setup+jndi+db+connection+scala+Lift+boot&source=bl&ots=iH22RoqdjR&sig=0JxSuJmjjPRiDbDzv3A8CbCLSOM&hl=sv&sa=X&ved=0ahUKEwiV8NiswNHLAhUKOpoKHY1sB6EQ6AEINjAD#v=onepage&q=setup%20jndi%20db%20connection%20scala%20Lift%20boot&f=false

best regards Peter Petersson
--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.

Kaiyin Zhong (Victor Chung)

unread,
Mar 21, 2016, 6:06:11 AM3/21/16
to lif...@googlegroups.com

2016-03-21 10:58 GMT+01:00 Peter Petersson <peterss...@gmail.com>:
DefaultConnectionIdentifier.jndiName = "jdbc/dsliftbook"

DefaultConnectionIdentifier.jndiName is deprecated. I did this in default.props:

default.jndi.name=jdbc/dsliftbook 

Please see the github repo.

Kaiyin Zhong

unread,
Mar 21, 2016, 9:40:00 AM3/21/16
to Lift
Also, using DefaultConnectionIdentifier.jndiName = "jdbc/dsliftbook" does not solve the problem.

Kaiyin Zhong

unread,
Mar 21, 2016, 1:07:28 PM3/21/16
to Lift
Aghrrrrrrrrr, there was a typo in the book, it should be "data-lift", instead of "data-list". Wasted so much time. If only html is statically typed....
(Going back to cursing the hell loose...)

Peter Petersson

unread,
Mar 21, 2016, 1:18:58 PM3/21/16
to lif...@googlegroups.com
:) Awesome and not so awesome ;) I was just about to take a closer look at your code

good thing you solved the issue

best regards Peter Petersson
-- -- Lift, the simply functional web framework: http://liftweb.net Code: http://github.com/lift Discussion: http://groups.google.com/group/liftweb Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code --- You received this message because you are subscribed to the Google Groups "Lift" group. To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.

Diego Medina

unread,
Mar 21, 2016, 6:49:07 PM3/21/16
to Lift
That's a common typo once you use Lift for a while, that and also typing life instead of Lift :)

I just emailed the author of the book, hopefully he gets to submit updates to the book (won't help printed books but maybe ebooks will get the update)

Thanks for posting what the issue was

Diego



--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Diego Medina
Lift/Scala Consultant
di...@fmpwizard.com
http://blog.fmpwizard.com/

Kaiyin Zhong (Victor Chung)

unread,
Mar 22, 2016, 5:49:10 AM3/22/16
to lif...@googlegroups.com
Great. You are wonderful. :)


Best regards,

Kaiyin ZHONG


You received this message because you are subscribed to a topic in the Google Groups "Lift" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/liftweb/C3LRtkKMPC8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to liftweb+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages