I'm writing an application for Android using DBFlow as ORM library. So, here is one of my tables model:
#Table(database = Database.class)
public class Language extends BaseModel {
#PrimaryKey(autoincrement = true)
long id;
#Column
String key;
#Column
String title;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
/* .. Other setters and getters .. */
}
Everything works pretty good, but when I take a look at my DB inspector (I'm using Stetho), I can see 2 identical "id" column:
Its a little bit embarrassing and redundantly.. Isn't it? Is it OK, and what is the cause of this behavior? And if it is not OK, how can I do it right?
So, looks like its Stetho-side feature/bug (according this issue). Just ignore it in production.
Related
GreenDAO is searching for getters and setters with the "m" prefix included in the name in its generated classes.
For example, I've set the entity to not generate any getters and setters but it still looks for, let's say, getMDate() for my private Date mDate, instead of my manually created getDate() method. Same goes for setters.
This is just breaking my adherence to the Android Code Style Guidelines.
Is there any way to configure greenDAO to use the code style standards that I've set out in Android Studio when it generates code?
EDIT: Just to make things clearer, I've added some code to get my point across. I wrote the above question half knocked out on antihistamines due to having chronic hayfever while I was trying to work and just wanted to get to sleep so I apologise if it wasn't enough to work with.
#Entity(generateConstructors = false, generateGettersSetters = false)
public class Day {
#Id
private long mId;
#Unique
#NotNull
private Date mDate;
#ToMany(referencedJoinProperty = "mDayId")
private List<Goal> mGoals;
public Day(long id) {
this.mId = id;
mGoals = new ArrayList<Goal>();
}
public Day(long id, Date date) {
this.mId = id;
this.mDate = date;
}
public long getId() {
return mId;
}
public void setId(long id) {
mId = id;
}
public Date getDate() {
return mDate;
}
public void setDate(Date date) {
mDate = date;
}
public List<Goal> getGoals() {
return mGoals;
}
public void setGoals(List<Goal> goals) {
mGoals = goals;
}
}
Above is my Day class. As you can see I've disabled generation of the getters and setters in the #Entity annotation, and put my own in there. I've set up Android Studio to take the "m" into consideration when I use alt+enter to create getters and setters for each of my fields.
public Date getMDate() {
return mDate;
}
public void setMDate(Date mDate) {
this.mDate = mDate;
}
Here is an example of the getters and setters that greenDAO generates for my code given the field private Date mDate. This is breaking the code style guidelines in that the local variables include the "m" prefix and also that the method names include it as well (maybe a bit of a nit-pick there, but you can set up Android Studio so it doesn't do that which makes me think it shouldn't be like that).
With my getters and setters there, greenDAO still thinks the getters and setters are missing, which results in them being added twice. Once as the ones I put in, and another as what you see above. This also results in the generation of the code below.
#Override
protected final void bindValues(DatabaseStatement stmt, Day entity) {
stmt.clearBindings();
stmt.bindLong(1, entity.getMId());
stmt.bindLong(2, entity.getMDate().getTime());
}
#Override
protected final void bindValues(SQLiteStatement stmt, Day entity) {
stmt.clearBindings();
stmt.bindLong(1, entity.getMId());
stmt.bindLong(2, entity.getMDate().getTime());
}
Here is some of the code from the class DayDao which is generated from greenDAO. It's still using the names for the getters (and also setters) that it would have used to generate its own getters and setters (getMDate() instead of getDate()) if I didn't disable their generation on the Day entity class. I can't change this code because it just switches back the next time I build the project, and there is my problem.
My question is: how can I get greenDAO to take the "m" prefix thing into consideration when it generates its code and get it to use the getters and setters that I have set out myself? Or even get it to generate getters and setters itself without the "m" being included in the name and local variables?
The solution that I decided to use for this ended up being rather disappointing.
It involved just making my properties all public instead of private. Still conforming to the code style, these properties would no longer use the m prefix ... or any prefix, for that matter. It's not a very satisfactory solution as now my data can be accessed and set directly, just for a simple naming convention to be conformed to.
I (personally) disagree with people saying that using naming conventions hurts readability. If you structure your code well enough and give enough of a description in Javadoc comments and the like, you'll know exactly what something is doing. Having said that, if using the conventions is going to create such code as in my question above, then I guess that this is a situation where it does, and a reason not to use the code style in my other projects and still stick to the access that I want for my data.
So, I have two models: Document and Item. Table creation and insertion works just perfect. What I want to know is that if I do something like this:
mDaoSession.getDocumentDao().deleteInTx(selectedDocuments);
//or
mDaoSession.getDocumentDao().deleteByKeyInTx(documentIds);
Will any of queries above delete all Items related to this Document or should I do it manually (with additional code)? If not deleting is there any way in GreenDao to accomplish that?
Document.class
public class Document {
#Id(autoincrement = true)
private Long documentId;
#ToMany(referencedJoinProperty = "id")
public List<Item> items;
}
Item.class
public class Item {
private Long id;
}
I use jackson and sugar orm and i have some errors when parsing. The id field is located in the json constantly 0. What can I do to fix it?
This example my class:
#JsonIgnoreProperties(ignoreUnknown = true)
public class JsonScienceEvent extends SugarRecord<JsonScienceEvent>{
#JsonProperty("id")
private String eventId;
public JsonScienceEvent()
public JsonScienceEvent(String eventId){
this.eventId = eventId;
}
public String getEventId(){
return eventId;
}
the fieldid is inherited from the super class SugarRecord<T> along with the setter and getter methods setId(Long id) and getId().
You can override the id field generated by the Sugar library, but as far as i can remember it uses Long type so if you can change from String identifier to Long all should be fine and, this way you can force the library to use the id you're setting with the setter setId(Long id),
Sugar ORM actually creates its own ID field to maintain. If you're not inserting a value into the eventId field when creating a record, then your column is empty.
Try using "getId()" to get the auto incremented ID from the record. Don't forget to cast to a string if that's what you want back!
So, I have a few OrmLite 4.41 model classes which are initially being populated by GSON (simplified for clarity)
public class Crag {
#DatabaseField(id=true) private int id;
#ForeignCollectionField(eager=true, maxEagerLevel=2) #SerializedName("CragLocations")
private Collection<CragLocation> cragLocations;
}
public class CragLocation {
#DatabaseField(id=true) private int id;
#DatabaseField private int locationType;
#DatabaseField(foreign=true, foreignAutoCreate=true, foreignAutoRefresh=true)
private Crag crag;
#DatabaseField(foreign=true, foreignAutoCreate=true, foreignAutoRefresh=true)
private Location location;
}
public class Location {
#DatabaseField(id=true) private int id;
#DatabaseField private BigDecimal latitude;
#DatabaseField private BigDecimal longitude;
}
I'm then testing that things are happening as I expect...
#Test
public void canFindById() {
Crag expected = ObjectMother.getCrag431();
_repo.createOrUpdate(template431);
Crag actual = _repo.getCragById(431);
assertThat(actual, equalTo(template431));
}
and they aren't equal... why not? because in the object created by GSON (in ObjectMother.getCrag431()) the cragLocations field of Crag is an ArrayList and in that loaded by OrmLite it is an EagerForeignCollection
Am I missing a trick here? Is there a way to tell OrmLite what type I want that Collection to be? Should I just have a method that returns the collection as an arraylist and test for equality on that?
Thanks in advance
Is there a way to tell OrmLite what type I want that Collection to be?
There is no way to do this. When your Crag is returned by ORMLite, it is either going to be an EagerForeignCollection or LazyForeignCollection.
Should I just have a method that returns the collection as an arraylist and test for equality on that?
I assume in your Crag.equals(...) method, you are testing for equality for the cragLocations field as this.cragLocations.equals(other.cragLocations). This is not going to work because, as you guess, they are different types.
If you need to test equality you can extract both of them as an array. Something like:
Array.equals(
this.cragLocations.toArray(new CragLocation[this.cragLocations.size()]),
other.cragLocations.toArray(new CragLocation[this.cragLocations.size()]));
guys I'm very new to the Java word, but i share part of the knowledge because of my c# background, anyways i started developing for android and I'm running into a few snags like the following.
I usually program very OOP so i made all my objects and now i got a very common public class User with things like:
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
and many other properties in it. Also i have a class UserHelper
where i have a populated array with all the users been pulled by the queries in the Helper
public ArrayList< User > getCurrentUsers() { return currentUsers;
}
well... the thing is, i want to be able to populate a spinner with a the value returned from getFirstName as the Display and getId obviously as the Id. I know exactly how to do this in C# but i been trying to fight with Cursors and doing some reading around but nothing, so i figured that it would be an interesting question.
ANYONE CAN SHOW ME HOW TO DO IT PLEASE?
Hi Alex look this examples
http://developer.android.com/resources/tutorials/views/hello-spinner.html
http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views