greenDAO
greenDAO copied to clipboard
Default Values for Entity Properties
See https://groups.google.com/forum/#!topic/greendao/cbbUkP6DdHs for a discussion.
Using SQLite's DEFAULT values probably won't work because greenDAO bind's all values for inserting.
Probably, the values should be set in the constructor
I have another suggestion for this topic, see discussion
+1 on this feature.
In general, having a more flexible KEEP mechanism would a large majority of my issues. I think that KEEP within methods is one way to do it, but I would personally love to have a way to override generated methods, including constructors. Accidental overrides could be avoided with annotations, if that's a concern.
Example use cases:
- Add initialization inside constructors
- Create property representations are stored in a certain format in the DB, but accessed as Objects from code.
Another +1 it would make some stuff easier indeed ...
Another +1 (sad to hear this request have been added 3 years ago :-( )
+1 for this, It is good to have default values. Why not sure about this?!!!!
+1 so that all properties should have default values
+1 I also want this feature.
+1 this is a must.
+1. Yes, this is required as Double, Integer, Float, Boolean and other Wrapper datatypes are null when retrieved. It should be 0 or false in case of Boolean. This is highly required feature. GreenDao is a very good library and I don't wanna stop using it for such small features missing.
+1. Also primitive type like int
instead of Integer
will be welcome.
+1 for this feature. I'd rather not set the defaults in the constructor with the generator overwriting code.
+1 for this feature.
+1 this one is really missing.
+1 I also want this feature.
+1 for this feature!
+1 a much needed feature
+1 for this feature
I love greendao. please
+1 Please add
+1 I also want this feature.
+1 needed for alot of us please add it
OK, let's collect some requirements!
Something I really want to avoid is to use defaults managed by the database. Why? Because it would require querying the database after inserts/updates, which would add complexity and slowness. Not an option.
What would be pretty easy to implement is setting fields in an entity's default constructor to default values. Would that be an option?
having this in the constructor will not prevent putting NULL values. how exactly is this adding queries?
If the dev really wants to override with null, then why not?
The problem with letting the DB do it, is that we only might assume we have the current state (a certain default value). For example, if a dev updated the default value, but forgot about schema migration, greenDAO would have the new default value, but the DB would be actually using the old one. So this is dangerous. To prevent this, it would be an option to refresh the entity from the DB after insert/update to ensure a consistent state.
Btw, if the problem is that you have nulls because of fields are of non-primitive types (e.g. Long/Integer/Float/Double): this is a different topic! Please be aware that you also can use primitive types. In that case, "defaults" will be 0 and not null. If you use the generator project, you need to enable it using notNull() on your property.
since the defaults managed by Database is Not an option and it will affect the performance. You may add it to the contractor as well.
what I do for now:
public SurfaceEntity() {
// KEEP CONSTRUCTOR-DEFAULT - put your custom constructor code here
created = new Date();
myOtherDefaultValue = false;
SurfaceIsTransparent = false;
// KEEP CONSTRUCTOR-DEFAULT END
}
public SurfaceEntity(Long id) {
// KEEP CONSTRUCTOR-ID - put your custom constructor code here
this(); // call default to initialize with default values
// KEEP CONSTRUCTOR-ID END
this.id = id;
}
you may add function for the properties to this for us.
something like:
surfaceEntity.addBooleanProperty("SurfaceIsTransparent")
.setDefaultBool(false);
This is just a suggestions if it is possible.
Thank you
For greenDAO 3, it might look like this:
@Property(defaultValue="true")
boolean valid;
@Property(defaultValue="new Date()")
Date created;
So a default value would simply be a string pasted where needed (e.g. constructor, if that proves to be sufficient for most people).
@greenrobot When is this feature expected to be available ?
@greenrobot any updates ?
Hope it will be realised soon. Highly required thing.
the feature updates?
+1 for this, any news?
+1 , come on
+1
+1
+1 It's been 2years now..come on
Just to note: with greenDAO 3 it is easy to have a custom constructor (annotate the generated one with @Keep
) where you could set your default values. You may also modify your getters and setters to set or return some default value if appropriate. -ut
@greenrobot-team please provide me a clear example. thank you very much
@greenrobot you meat get default in java code, actually the default value is not in databases file ?
@greenrobot @greenrobot-team So how to solve the default value issue when it comes to migration?
Caused by: android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: Books.IS_MOST_READ
your solution is not working and not clear, you don't even provide us with a working example!
+1
Hey is this gonna happen or what?
+1
+1