[Lift] Re: JPA find or create new
That will probably work. I was thinking it would be nice to build in a method findOrNew that would do it for me... but it looks like that might involve some sort of implicit manifest thingy, so I don't know. Chas. Viktor Klang wrote: On Mon, Dec 1, 2008 at 7:12 AM, Charles F. Munat [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Is there a simple way in JPA/Lift to query to retrieve a single object from the database, assign it to a val if found, or create a new object of that type and assign it instead of there is no such object in the database? Sort of a... val user: User = Model.createNamedQuery( findUserByUsername, username - hal ).findOr(new User()) (Can ! (yourQuery.uniqueResult)).openOr(new User()) Should work? or something like that? That would be very useful. Thanks, Chas. -- Viktor Klang Senior Systems Analyst --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: JPA find or create new
Because it's type erasure I don't think I can do a generic new inside the methods, but the findOne method does return a Can. That means that you were actually pretty close in your demo code. Here's what it could look like: val user: User = Model.createNamedQuery[User]( findUserByUsername, username - hal ).findOne or (new User) Derek On Mon, Dec 1, 2008 at 2:24 AM, Charles F. Munat [EMAIL PROTECTED] wrote: That will probably work. I was thinking it would be nice to build in a method findOrNew that would do it for me... but it looks like that might involve some sort of implicit manifest thingy, so I don't know. Chas. Viktor Klang wrote: On Mon, Dec 1, 2008 at 7:12 AM, Charles F. Munat [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Is there a simple way in JPA/Lift to query to retrieve a single object from the database, assign it to a val if found, or create a new object of that type and assign it instead of there is no such object in the database? Sort of a... val user: User = Model.createNamedQuery( findUserByUsername, username - hal ).findOr(new User()) (Can ! (yourQuery.uniqueResult)).openOr(new User()) Should work? or something like that? That would be very useful. Thanks, Chas. -- Viktor Klang Senior Systems Analyst --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: JPA find or create new
Thanks. Types are still largely a mystery to me, but this works just fine. Chas. Derek Chen-Becker wrote: Because it's type erasure I don't think I can do a generic new inside the methods, but the findOne method does return a Can. That means that you were actually pretty close in your demo code. Here's what it could look like: val user: User = Model.createNamedQuery[User]( findUserByUsername, username - hal ).findOne or (new User) Derek On Mon, Dec 1, 2008 at 2:24 AM, Charles F. Munat [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: That will probably work. I was thinking it would be nice to build in a method findOrNew that would do it for me... but it looks like that might involve some sort of implicit manifest thingy, so I don't know. Chas. Viktor Klang wrote: On Mon, Dec 1, 2008 at 7:12 AM, Charles F. Munat [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Is there a simple way in JPA/Lift to query to retrieve a single object from the database, assign it to a val if found, or create a new object of that type and assign it instead of there is no such object in the database? Sort of a... val user: User = Model.createNamedQuery( findUserByUsername, username - hal ).findOr(new User()) (Can ! (yourQuery.uniqueResult)).openOr(new User()) Should work? or something like that? That would be very useful. Thanks, Chas. -- Viktor Klang Senior Systems Analyst --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: JPA find or create new
On Mon, Dec 1, 2008 at 7:12 AM, Charles F. Munat [EMAIL PROTECTED] wrote: Is there a simple way in JPA/Lift to query to retrieve a single object from the database, assign it to a val if found, or create a new object of that type and assign it instead of there is no such object in the database? Sort of a... val user: User = Model.createNamedQuery( findUserByUsername, username - hal ).findOr(new User()) (Can ! (yourQuery.uniqueResult)).openOr(new User()) Should work? or something like that? That would be very useful. Thanks, Chas. -- Viktor Klang Senior Systems Analyst --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---