Comments (15)
Salat does support polymorphic collections. Please update to the latest snapshot, try again and include the text of your compiler error?
from salat.
Oh right. You want to type ChildCollection to an abstract superclass.
We've gone back and forth on allowing that due to its runtime reflection requirement. I'll change the typing and write some specs this weekend, but you should be aware that it will not be as fast as a DAO typed to a concrete class. Will the speed make a difference to your project? Most likely not, but under very heavy usage everything counts, which was our original design intent in not putting it in...
I'll let you know when an updated snapshot is up for you to try.
(Also, Role
should have its own _id field, in addition to the "parent id" field - although MongoDB Java driver will insert an ObjectId for you if no _id field exists, it is best practice to add one yourself, and has the side benefit of making child collection "by id" methods work.)
from salat.
Cool thanks, looking forward to it.
So your advice would be not to use SalatDAO for polymorphic collections in performance-critical code?
from salat.
Well, for everyday usage it's probably fine. To give you more
background on why Salat was developed, I work at a financial startup
where we do realtime analytics that can require large datasets. In
this case, the time I spend deserializing my data back into my domain
model before I begin doing the calculations is sheer overhead. So I
wouldn't use ProxyGrater under those circumstances.
But we have been moving more toward providing the polymorphism support
people want, because in many use cases the convenience outweighs the
runtime hit - you can see ProxyGrater to get an idea of the added
cost. Grater just serializes and deserializes: done. But ProxyGrater
needs to look up a concrete instance of Grater based on the type of
the incoming/outgoing object, which adds a small continuous cost. If
you're interested, compare the asObject and asDBObject methods in
ProxyGrater and ConcreteGrater:
https://github.com/novus/salat/blob/master/salat-core/src/main/scala/com/novus/salat/ProxyGrater.scala
https://github.com/novus/salat/blob/master/salat-core/src/main/scala/com/novus/salat/Grater.scala
I suggest trying out the child collection typed to Role, and if the
performance is not good enough, consider making Role a case class and
using a sealed abstract class/trait and case object hierarchy to
distinguish different types of role.
I'll have a new snapshot for you tomorrow.
On 21 January 2012 19:22, Marius Soutier
[email protected]
wrote:
Cool thanks, looking forward to it.
So your advice would be not to use SalatDAO for polymorphic collections in performance-critical code?
Reply to this email directly or view it on GitHub:
https://github.com/novus/salat/issues/27#issuecomment-3600149
from salat.
Thanks, I'll definitely check it out. I guess for user management I don't need extra performance. But there's also one area in my app where database-object-mapping-performance will matter, so I'll dive into it.
My other big concern so far is the tight coupling between my model classes and the MongoDB classes. But in the end that's just the same problem with relational modeling.
from salat.
Also, Role should have its own _id field, in addition to the "parent id" field - although MongoDB Java driver will insert an ObjectId for you if no _id field exists, it is best practice to add one yourself, and has the side benefit of making child collection "by id" methods work
Somehow, this doesn't work. The resulting child collection doesn't have any id at all.
I get something like this:
{ "_id" : { "$oid" : "4f1c63df0364894c96ac18c2" }, "_typeHint" : "models.User", "username" : "[email protected]", "password" : "sdsdf", "email" : "[email protected]",
"roles" : [ { "_typeHint" : "models.AdminRole", "someInfo" : "hi" } ], "gender" : 1 }
I added the id directly to the role:
@Salat sealed abstract class Role(profileid: ObjectId = new ObjectId)
case class AdminRole(someInfo: String) extends Role
from salat.
Hi Marius, your snapshot is now available for testing. After running sbt update
and your original code should compile without type errors now. Let me know how that works for you.
See novus@9553d12 for details.
The reason why you are not seeing _id
in Role
above is that it is not actually added until you insert it into the MongoDB collection - this is not done by Salat, nor Casbah, but rather deep in the guts of mongo-java-driver.
from salat.
Unfortunately it's not in the snapshot. The date in http://repo.novus.com/snapshots/ is at 29-Nov-2011.
The reason why you are not seeing _id in Role above is that it is not actually added until you insert it into the MongoDB collection
The BSON from above is from the database. I saved it, but the id from the abstract class is missing.
from salat.
I published 2.8.1 and 2.9.1 - which snapshot are you using?
On Jan 24, 2012 7:19 AM, "Marius Soutier" <
[email protected]>
wrote:
Unfortunately it's not in the snapshot. The date in
http://repo.novus.com/snapshots/ is at 29-Nov-2011.The reason why you are not seeing _id in Role above is that it is not
actually added until you insert it into the MongoDB collectionThe BSON from above is from the database. I saved it, but the id from the
abstract class is missing.
Reply to this email directly or view it on GitHub:
https://github.com/novus/salat/issues/27#issuecomment-3631383
from salat.
http://repo.novus.com/snapshots/com/novus/salat-core_2.9.1/0.0.8-SNAPSHOT/ - this shows a timestamp of 23 January for the 2.9.1 snapshot.
from salat.
Ok, it looks like you are using the 2.9.0-1 snapshot. Please use 2.9.1 instead.
from salat.
Ok I used the wrong one (2.9.0_1). Everything works now, thank you.
from salat.
Here's a gist that shows what I mean when I say the mongo-java-driver will add an _id
field if you don't do it yourself:
https://gist.github.com/1670017
from salat.
Glad to hear everything is working!
from salat.
Thanks for the gist. My problem was that Mongo didn't generate any _id for an embedded document (I originally tried to embed the role, because without the user it doesn't make much sense). With the separate collection, the id is generated, and I also added it -as you advised- to the abstract class (had to be a val
to be recognized).
Thanks for your quick support, much appreciated!
from salat.
Related Issues (20)
- Custom TypeHint strategy for JSON HOT 2
- Unable to serialize case class hierarchy HOT 3
- mongo connection leak - re-use one mongo connection for each test DAO to reduce connection footprint. HOT 2
- Update Casbah to 2.8.2 in Salat 1.10.x to support connecting to Mongo 3.x server. HOT 16
- Play Salat HOT 4
- Project status update? HOT 23
- java.lang.ClassCastException when deserializing from Salat in context of akka runtime. HOT 4
- Error with reason "Very strange!" - GRATER GLITCH HOT 2
- Upgrade to sbt 0.13.12
- Release Salat 1.10 HOT 1
- JSON data can deserialize into collections containing data not matching the container generic type.
- More specific error class for Grater errors: SalatDeserializationException
- Release 1.11.x with Casbah 3.1.1 support (but not Scala 2.12) HOT 18
- Will this work with the latest Casbah 3.1.1? HOT 1
- SalatDAO support for collections HOT 7
- Scala 2.12 HOT 8
- Optional field in MongoDBObject with null value causing MatchError HOT 11
- Custom Transformers never get applied to case classes HOT 9
- Case class not serialising in order HOT 3
- Support for Mongo 3.6+ HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from salat.