android


ClassCastException when primarykey is short in greendao


I created an entity which need to identity by Short only.
Here is my generated code:
public Source(Short id, String name) {
this.id = id;
this.name = name;
}
TestCode DatabaseHelperTest.java:
public void testInsertAndLoad(){
Source source = new Source((short) 0, "TestSource");
SourceDao sourceDao = daoSession.getSourceDao(); //#line 26
sourceDao.insert(source);
Short id = source.getId();
assertNotNull(id);
}
When I run test, I got the ClassCastException:
Running tests
Test running started
java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Long
at de.greenrobot.dao.identityscope.IdentityScopeLong.put(IdentityScopeLong.java:31)
at de.greenrobot.dao.AbstractDao.attachEntity(AbstractDao.java:695)
at de.greenrobot.dao.AbstractDao.updateKeyAfterInsertAndAttach(AbstractDao.java:362)
at de.greenrobot.dao.AbstractDao.executeInsert(AbstractDao.java:355)
at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)
at com.tuanchau.DatabaseHelperTest.testInsertAndLoad(DatabaseHelperTest.java:26)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
So, does GreenDAO allow to make short become primary key?
And, how can I deal with this exception.
Thanks
Update:
DB Generation code
Entity source = schema.addEntity("Source");
Entity category = schema.addEntity("Category");
source.addShortProperty("id").primaryKey().getProperty();
source.addStringProperty("name").notNull();
category.addIntegerProperty("id").primaryKey().getProperty();
category.addStringProperty("name").notNull();
Property csid = category.addLongProperty("sid").notNull().getProperty();
category.addToOne(source, csid);
Source Properties
public static class Properties {
public final static Property Id = new Property(0, Short.class, "id", true, "ID");
public final static Property Name = new Property(1, String.class, "name", false, "NAME");
};
Category Properties
public static class Properties {
public final static Property Id = new Property(0, Integer.class, "id", true, "ID");
public final static Property Name = new Property(1, String.class, "name", false, "NAME");
public final static Property Sid = new Property(2, short.class, "sid", false, "SID");
};
From greenDao website:
Current primary key (PK) restrictions: Currently, entities must have a long or Long property as their primary key. This is recommended practice for Android and SQLite. greenDAO is prepared to handle any primary key scenario in the future, but not everything is implemented completely yet. To work around this issue, you can use a long primary key and use an unique index for the intended “key” properties.
You could try using something like this:
source.addIdProperty();
source.addShortProperty("shortId").unique().getProperty();
source.addStringProperty("name").notNull();

Related Links

Custom DNS on an Ad Hoc network
Querying in-app product inventory without connectivity
Is a Dialog asynchronous on Android?
Android Volley makes 2 requests to the server when retry policy is set to 0
Android: How to display custom keyboard in SearchView?
Adding views to the layout programatically (and inflating) is very slow
Double sound android
Send Data from an Activity to a class in Android
inflate another layout below in android
Wrong characters UTF8 with local json in Android
Volley: How to send JSONObject using bearer accesstoken Authentication
How can I close an opened HTML document in Android application?
Fragment not attached to activity after commit
Navigation In Ionic Angular.js
Failed to attach client certificate during ssl handshake in android
ImageView inside Framelayout onclick only fired on second click

Categories

HOME
microsoftgraph
gridview
apache-flink
jmeter
dns
pagination
pascal
battery
tvos
nuget-package
augeas
enthought
distribution
code-generation
zerobrane
flash-player
alpacajs
package.json
job-scheduling
feathersjs
html5-appcache
windows-xp
user-experience
visual-glitch
postscript
beautifier
surveymonkey
fragment-backstack
subclass
browser-sync
constexpr
gitlab-api
mobx
jmockit
laravel-eloquent
sql-delete
xajax
sql-server-ce
google-earth-engine
modernizr
projects-and-solutions
setup.py
comparison-operators
azure-arm
lpsolve
smoothstate.js
importerror
stylecop
oim
rfc3339
nanoc
jboss-4.2.x
ovf
zenhub
arq
aquamacs
video-conferencing
qtranslate
pdf-conversion
oozie-coordinator
cvxpy
change-tracking
taocp
meteor-packages
reporting-services-2012
neon
nikeplus-api
lua-telegram-bot
spreadjs
data-protection
yajsw
grass
exim4
sapi
zen-cart
text-search
evolus-pencil
mod-proxy
jdk1.7
branch-and-bound
jquery-forms-plugin
fits
java-security
ssis-data-flow
easyrtc
imagefilter
jqmodal
angular-carousel
named-parameters
personalization
live555
functional-java
jjaql
ray
gae-quotas
getopt
scmmanager
superscrollorama
curb
qi4j
cakephp-1.2
garbage
boost-signals
objectquery
josql
node-redis
xcelsius
rmdir
umra
metaweblog
office-2007
ajax-polling
burndowncharts
duplex
cuda.net
software-lifecycle
firefox-3
cardspace

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App