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
Add upsert and/or fetchByKey(Class, Object...) methods to Session #11
Comments
The simple fetch you see commented out is because the overload collides if you have a primary key that is a string. The usual method is fetch(class, sql, params) so you can just use fetch(class, "select * from table where id = ?", 123) Or if you know the primary key value to find it Customer customer = new Customer(); if (session.fetch(customer)) { But you're right. I could have an "upsert" method. Yeah I've been looking at that fetch method. I was thinking of writing a Parameter class to capture the parameters as a type. Then later add named parameters as a feature. I know it would break the existing API but it might be OK in a 1.1 or 2.0 release to do that. Also I'm adding a withTransaction() which simplifies transactions and getting records to work. ;) In terms of writing the SQL yourself. I consider that to be feature, not a bug. I think most ORMs fail on trying too hard to abstract away SQL. Good ideas! If I have time this weekend I'll work on getting Informix added to the test suite. |
Thanks for the quick response! [I definitely like to have the ability to write the SQL myself -- actually that's why I'm looking into alternatives to JPA like your library. We've implemented a webservice to abstract away various tables from different databases (and even other systems), but so far only for read (GET) requests. We use dbutils so far and some homebrewn framework to use some SQL-templates and "fill in" dynamically some additions to the WHERE clause, depending on the requests needs, and that has been working extremely well. (We have a parallel "sibling"-project with another team doing something similar, but using mostly JPA, and so far I like the plain-SQL-approach much better than fighting with the Criteria API or JPQL ...)] Anyhow: I'm looking for something generic regarding the CUD-part of CRUD now, as we need to write some things back into the database. And I'd very much like to have the SQL generated automatically for these cases ... I'll consider your proposal, but it requires me for my implementation of an upsert to not be able to write it generically for any POJO -- that's what's concerns me, as I'd like to write it just once. Another idea: Maybe we can better live without the fetch with parameters if there would be an exists-method, that doesn't touch the POJO, but merely checks if a record is in the database, e.g. like this:
Otherwise I need to know about all the details of the customer, the id, the table name etc. -- and that's actually all handled by Persism already for insert/update anyhow ... so why would I bother to do that manually?!? ;-) Actually implementing a generic upsert in Persism might benefit from some transaction handling to prevent race conditions, so I'm curious what you're up to. [Informix is working good so far for my simple test cases, so I'm already satisfied with that ;-) ...] |
I found a generic way to implement my upsert:
This could be added as an out-of-the-box solution, though I'm happy with it in my use case. And I'd still enjoy the |
FYI - The update would usually always return 1 unless you implement Persistable or extend PersistableObject since these do the check for changed fields. |
Can't confirm it's always reporting '1' -- it rather reports the number of affected rows, as JDBC does. I"m not implementing Persistable yet. |
I would recommend for now to use if (fetch()) which returns true if found. Or use the other one and check for null. |
Right now I'd looking for a generic way without needing to write individual SQLs for it, and as fetch() overwrites the original content, I'm not happy with that. It all calls for an I'll stick with my current solution for now and add a check for |
Check out my experimental branch. It's a new API. Let me know what you think. |
So regarding this issue we're mainly talking about this method, right? (I also found some more
I appreciate the automatic SQL-generation involved which then gets filled with the primary-key-parameters. I've been thinking about it a little and looked into the source code. If there's only one primary key column this will be fine. While this doesn't need to be a problem, there are two concerns:
Thinking about it I start to appreciate the original fetch-method that gets a POJO in a QBE-style, as the order of the columns isn't relevant there. Here are some options that come to mind:
What do you think about it? Thanks for putting efforts into this issue! |
NON standard sql. |
Don't know how this feature request is dependent on an Sql upsert command. |
Hi, hope you're doing well! Yeah MySQL for example has REPLACE. |
I'm just exploring this library on Informix and it's basically working though it doesn't have "official" support - so that good :-).
What I'm missing is a kind of upsert-statement: if an object is alread in the database I'd like to update it, if it's not there I'd like to insert it, e.g.:
I tried to work around it by using fetch first and then deciding if I need to insert or update. But I couldn't find a method which I could pass in the requested class and the key, e.g.:
Customer customer = session.fetch(Customer.class, 5);
Actually -- I found exactly that method, but it's commented out, as it doesn't work together with the variant that accepts an sql as a second parameter)... so that's a method that I'm missing, too.
There is a fetch-method with a single POJO parameter, but it needs to be filled with the primary key first and when querying it, it will overwrite all contents contained in it. I could work around it by cloning the object, but that would require the POJO to be Cloneable, and it doesn't feel very right conceptually:
So I'm kind of stuck here. I can of course write a simple SELECT myself and use the fetch-method with the SQL as a second parameter, but that doesn't really fit into the concept of simplicity with zero ceremony ;-) ...
So I'd propose
So what do you think? Thanks for consdidering :-)
[Background: My usage szenario is that I'm offering a REST webservice where the consumer can request a PUT some special field value. This value is usually just some optional add-one to some other POJO. In the GET-request we simply provide a default value if there's no value available, but we need to offer the option to store something explicitly.]
The text was updated successfully, but these errors were encountered: