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

query() forces classname to match tablename #17

Closed
dstango opened this issue May 29, 2021 · 2 comments
Closed

query() forces classname to match tablename #17

dstango opened this issue May 29, 2021 · 2 comments

Comments

@dstango
Copy link

dstango commented May 29, 2021

Yesterday I ran into an - at first sight - obscure problem with the query()-method. Digging into it revealed what's going an.

The following query:

List<UnknownTable> l = session.query(UnknownTable.class,
"SELECT LIMIT 10 personalnr, bewerbernr, nlcode FROM mitarbeiter WHERE austrittsdatum < MDY(1,1,2018)");

results in an exception, unless the the database has a matching table name for "unknowntable":

net.sf.persism.PersismException: Could not determine a table for type: de.test.dali.persistence.UnknownTable Guesses were: [UnknownTable, UnknownTables, Unknown Table, Unknown_Table, Unknown Tables, Unknown_Tables]

If there is actually a table for "UnknownTable" the error message gets more obscure (as it was for us yesterday) - imagine you do a query with the parameter Bewerber.class and there actually is a table in the database with the same name:

net.sf.persism.PersismException: Object class de.test.dali.persistence.Bewerber was not properly initialized. Some properties not initialized by the queried columns: [bewerbernr, nlcode] Missing:[nl_code]

I didn't try it with joins, but I guess it will cause similar problems.

So what's would be expected behaviour:

  • at least an error message stating something like "Class name (or @Table annotation) needs to match the queried tabled" -- especially the second case left us totally clueless yesterday ...
  • but better: no error message but simply delivering the results of the query, independent of a match of the table name.

I think the meachanism to match table names to class names (or @Table annotations) is useful for CRUD of individual records -- but with a free query this should not be constraining the classes that can be used.

@sproket
Copy link
Owner

sproket commented May 30, 2021

I would use the @NotTable annotation for this. That's used for when you have an arbitrary query not associated to any particular table. In that case Persism doesn't look at table name and just matches up Column names. So where you want some query you need to create a Class or Record with matching property names.

@dstango
Copy link
Author

dstango commented May 30, 2021

Ah, I wasn't aware of that! Thanks!

@sproket sproket closed this as completed May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants