Fetch object by plain SQL query with SORM

不问归期 提交于 2019-12-10 03:19:02

问题


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]

回答1:


Since populating an entity with collections and other structured values involves fetching data from multiple tables in an unjoinable way, the API for fetching it directly will most probably never get exposed. However another approach to this problem is currently being considered.

Here's how it could be implemented:

val artists : Seq[Artist] 
  = Db.fetchWithSql[Artist]("SELECT id FROM artist WHERE name = ?", "Metallica")

If this issue gets a notable support either here or, even better, here, it will probably get implemented in the next minor release.

Update

implemented in 0.3.1




回答2:


If you want to fetch only one object (by 2 and more arguments) you can also do the following:

by using Sorm Querier

Db.query[Artist].where(Querier.And(Querier.Equal("name", "Name"), Querier.Equal("surname", "surname"))).fetchOne()

or just

Db.query[Artist].whereEqual("name", "Name").whereEqual( "surname","surname").fetchOne()


来源:https://stackoverflow.com/questions/13527340/fetch-object-by-plain-sql-query-with-sorm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!