Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default constructor necessary #56

Open
czellweg opened this issue May 8, 2015 · 2 comments
Open

Default constructor necessary #56

czellweg opened this issue May 8, 2015 · 2 comments
Labels

Comments

@czellweg
Copy link

czellweg commented May 8, 2015

Hey mate

Great library, we have just started using it and it works like a charm so far! Thank you for your continued effort.

I've just spend a while figuring out why I wasn't able to read data from the database as there was no exception and everything seemed to have worked. The issue turned out to be that there needs to be a default constructor for the mapping class, i.e. the class we wish to populate the values from the database with.

Long story short:
in com.datastax.driver.mapping.builder.MappingBuilder.getFromRow(Class<T>, Row):533 where you create a new instance based on the class object of the entity one wishes to map values to

...
// create PK
try {
      entity = clazz.newInstance();
      PrimaryKeyMetadata pkmeta = entityMetadata.getPrimaryKeyMetadata();
...

if there is no default constructor of the entity, the entity reference will remain null. The javadoc also mentions this: Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. Hence, this does not lead to an exception when we set the database values in the following for-loop

...
if (field.isPartition()) {
       field.setValue(partitionKey, value);
} else if (field.isPrimary()) {
       field.setValue(primaryKey, value);
} else {
       field.setValue(entity, value);
}
...

Suggestion:
In case a default constructor is absent, maybe throw an exception or log a statement so the user has a better idea what is going on?

@valchkou
Copy link
Owner

Thanks for comments, this is a valid concern.
I initially started project with the minimum or no custom logging and error handling.
But as more and more people use it I should consider adding meaningful messages to it.

Thanks for the story, I am sorry I have to figure it hard way

@wilsondy
Copy link

Also, for those who might follow, if you don't have any constructor defined, Java adds the default constructor, which means this package will work (similar to all the test cases). Once you add your own, the default one is removed and this issue will occur.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants