MyBatis - How can I define an update xml mapper, that returns values? - mybatis

I have the following xml mapper definition:
<update id="updateProductViews">
UPDATE products_views
SET views = views + 1
WHERE product_id = #{productId}
RETURNING views;
</update>
What I'm trying to get is the returning value of the SQL query within the mapper definition.
Unfortunately, mybatis doesn't allow me to pass the resultType="Integer" in an update mapper definition, and if I don't pass resultType attribute, I just get back -1, after each update.
How should I define an update mapper that returns a value?
Thanks
PS: views stores a bigint value. By DB is postgresql

Related

How to get return value from insert query defined in #query annotation

I have two tables city and shape, where city has composite primary key as(City and country) and one auto generated Id value. This Id is foreign key for Shape.
So after insertion of data in city table, I want Id that was inserted to be used in shape table. I have used Spring boot with jpa and postgres
In CityRepository I have custom save method which will do nothing on conflict.
I have tried below code to get returned value. But I get Error
SqlExceptionHelper : A result was returned when none was expected.
How to get the returning value from insert query?
#Modifying
#Query(value="insert into public.city_info(city, country) values(:city,:country) on conflict do nothing returning city_id",nativeQuery = true)
#Transactional
Integer save(#Param("city")String city,#Param("country") String country);
I'm afraid but that's not possible with JPA.
If you look at the JPA Query API:
int executeUpdate()
Execute an update or delete statement.
Returns: the number of entities updated or deleted
There is no other return value possible.

mvc Entity Framwork, insert using select max() from table

I am developing an App using MV5 and Entity Frameowrk to connect to SQL Server Database
I need to do an Insert where the is getting ID like this.
Select max(id)+1 from table where field_id = #ID.
The idea is to do it in one transaction.
Is that Posible to do it in Entity Framework or I should use a Store Procedure?
EF compiles the C# expressions you provide it into SQL, and then executes the resulting SQL. So you can write such a query in LINQ, which will in-fact be computed during a single "call" to the database:
long id = MyDbContext.MyDbSet.Max(entity => entity.Id) + 1;
Do note, that if you are doing this so that you can assign a non existing ID to a new entry- there's no need. EF takes care of this for you, so long as the field is named Id and it is of type long. Just do not assign it any value.

Inserting only assigned properties with EF6

Is it possible to get SaveChanges to produce an INSERT statement containing only columns that have assigned values for the associated properties.
For example:
administrator.Login = login
administrator.PasswordSalt = salt
administrator.Password = hashed
administrator.CreatedBy = "xxx"
db.Administrators.Add(administrator)
db.SaveChanges()
Should only have four fields in the INSERT statement. Right now, SaveChanges is adding all the fields, setting the unassigned properties to have a value of NULL, which prevents any default value being used. Example: CreatedDate has a default of getdate().
There are at least two different ways to exclude property from INSERT/UPDATE statement in compile-time:
1) EDMX: go to entity and set StoreGeneratedPattern option for a property to Computed or Identity.
If CodeFirst is used: try [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribite instead.
2) Add AFTER INSERT trigger to SQL table to update an inserted record with default values. How to update inserted field in trigger
I'm not sure it is possible to do in run-time in pure EF.

How to map a column to JSONB instead of JSON, with Doctrine and Postgresql?

In my project I'm using Doctrine doctrine 2.5.4/postgres 9.5. I'm trying to add a jsonb field using YAML:
fields:
obj: json_array
This gets interpreted as a json column type (and not jsonb). The specification notes about picking up json or jsonb:
Chosen if the column definition contains the jsonb option inside the platformOptions attribute array and is set to true.
But platformOptions doesn't seem to work (tried to add it below obj, at the top... with no success). How can I add a jsonb field?
This is supported by doctrine/dbal v2.6+ (it requires PHP 7.1). All you need to do is use json_array and set options={"jsonb"=true} I tested this on doctrine/dbal v2.6.3
This is what it looks like in PHP format:
/**
* #ORM\Column(type="json_array",nullable=true,options={"jsonb"=true})
*/
private $details;
and it creates a query such as (for an existing table):
ALTER TABLE mytable ADD details JSONB NOT NULL;
More details about type mapping can be found at Doctrine mapping matrix.
Use boldtrn/jsonb-bundle, it provides a jsonb doctrine type as well as custom functions to access the special operators provided by the PostgreSQL jsonb data type.

EF5 - modify generated insert/update statement before SaveChanges()

I need to modify the generated SQL for insert/update operation, before it is sent to database. The required modification is very specific, so I was hoping that there is a way to simply append string to statement.
For example, SQL looks like this (Oracle BTW):
UPDATE TABLE_A
SET DESCRIPTION = "ABC"
WHERE OBJECTID = 1
But I want to append this line (in SET part) to update one more field:
SHAPE = sde.st_geometry('point (18 57)', 4326)
I can't add SHAPE column to EF model, because that is unsupported data type.
Now, is there a way I can modify EF generated SQL statement?
You could move this update to a simple stored procedure that is mapped into your entity data model.