MyBatis: How to return the ID of the inserted object under Postgres? - annotations

I've got a postgres table where the ID is defined as bigserial. How
can I use #Insert and get back the id of the inserted entity? I am
expecting the mapper method to either return the id or populate the id
field inside the entity object. Any ideas?

The mapper will return you the number of records that were actually inserted.
In order to get back the id of the inserted record, you'll need to add a second annotation (that will populate the id) :
#Options(useGeneratedKeys=true, keyProperty="idSomething")
Note that keyProperty is not necessary if the identifiyng property is named "id" in your entity object.

NVM, i think i found the answer on the other thread,
http://mybatis-user.963551.n3.nabble.com/How-to-return-the-ID-of-the-inserted-object-under-Postgres-td1926959.html
There's the link for anyone else who lands here.

Related

virtual column field not updated after entityManager.merge()

I have below scenario while updating Person entity..
My initial entity:
firstName:x
preferredFirstName:y
name:y (this values is calculated in the database. the logic is if preferredFirstName has value then name = preferredFirstName, else name = firstName)
updated entity:
person.setFirstName("a");
person.setPreferredFirstName("b");
now when I do em.merge(person); firstName and preferredFirstName are changed to a and b. But the name field in person entity still holds “y”
I tried:
flush
em.find(person, id)
refresh
None of them seems to be working.
Does any one better way of getting the updated value of the virtual column?
doing em.flush() followed by em.refresh() did it.

getting Greendao to return object on insertOrUpdate?

When using the insertOrUpdate method in Greendao, is it possible to return the inserted object. I basically want to get at the newly created key.
I see references to rowId but i don't think that is going to help me...
You can use the returned rowId to get the entity with: EntityDao.loadByRowId(rowId). From the entity you can then get the key with EntityDao.getKey(entity).

Entity Framework 4.1: HOWTO know the next identifier assigned by Database automatically

I have POCO objects which their identifiers are unique and generated automatically by the database, so the problem is when you want to know for some reason which will be the next identifier that the database is going to assign to the next record you are inserting. As far as I know it is only possible after performin dbContext.SaveChanges() so I would like to know if I am right or is there a way to know the next identifier assigned by database automatically.
is there a way to know the next identifier assigned by database
automatically
Well, the next one NO. And if your code depends on it, you really need to change your design.
If you need the identifier to insert related objects, you should check some other questions because you can assign entities to eachother instead of ID's and it will be fine.
I agree with the general purport of the comments that having to know an identity value is "smelly". But on the other hand sometimes you have to live with a given design.
You can't really get the value of the next id, but you can get the value of the assigned id in time by using a TransactionScope.
using (var trans = new TransactionScope())
{
// Create new object
...
context.SaveChanges();
int id = newEntity.Id;
dependentEntity.IdString = string.Format("{0:0000000}", id);
context.SaveChanges();
trans.Complete();
}

Is possible to achieve inserted row's Id that added by EF

I have a table in EF that in it Id column is PK and Auto_Increment. I use this code to insert a row to table:
Cut newCut = new Cut()
{
Name = name,
Comments = comments
};
context.Cuts.AddObject(newCut);
context.SaveChanges();
Is possible to achieve added row's Id without another query?
Just try newCut.Id after SaveChanges()
When you save changes, your Cut instance automatically populates with new Id by entity framework. So after SaveChanges() you can call newCut.Id.

Entity Framework SET IDENTITY_INSERT

Is there a way to force the ID value for a new entity in EF when we have an auto-incrementing ID column, i.e. use SET IDENTITY_INSERT behaviour through EF?
Our requirement is that our create form must always show a new, unique ID for the object we're creating on the empty form before it is filled in or saved. The idea is that this ID can be out read to someone over the phone and then the user can complete and save the form after the call is complete. We could reserve an ID by inserting an empty row into the database there and then, but we have unique columns and FKs; instead I've made a 'next ID' table that we increment with locks for safety, and I test this against the top ID in the object table too to be careful. The idea was to then force the use of this new ID when we write back the entity - but I can't see how to get EF to do it.
Is that possible - is it just something I've missed? I don't think the ID even makes it down to the insert so I don't think manually calling SET IDENTITY_INSERT around the SaveChanges would help.
Or do I have to do something else? I can see alternatives:
Change our ID column to not be an identity and take manual control of it all: there's a table ID inheritance here so this is potentially tricky too.
Separate DB ID and user-visible ID into a separate column, and record our unique ID there.
Empty row to reserve the ID, as above; might need some nullability changes, and amending our data read code to ignore these records.
Thanks! This is EF4 (using an EDMX and generated classes not POCOs), and against SQL Server 2008 in case that matters.
Why not use a Guid as primary key. Nothing to do with auto-increment, no concurrency pitfalls etc. You just create the Guid at the moment you create the form. Hand it over to a caller and fill in the form afterwards. When the form is cancelled, no problem. When the form is finished create the entity with the created Guid set the other values of the entity object, apply it to the (a) context and SaveChanges()...
Alternatives that wont alter your schema
Use EF Transaction
You can call context.SaveChanges() and get the autoincremented primary key. Once the process is completed you can commit the transaction. If the transaction is cancelled or there is an error/exception, you can always rollback so you wont have holes/dirty-data in your rows. I suggest you use the singleton pattern and pass the same transaction/context to whatever methods or screens to complete the process.
Just add an additional status: Draft
Save empty form as draft with saved ID, then proceed to edit the form with the information. Once complete save the form as final/ready. If you wont proceed to save the form, you can always recycle the draft.