One to One Inserts

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

One to One Inserts

Anand Joshi
I have 2 tables Account and Person with one to one relationship. Account has user login credentials and Person has more like Profile Information like name dob etc.
Account.id and Person.id are autogenerated values. Person.id is FK into Account.person_id.
So when I get a request I need to insert Person record first, obtain Person.id then insert Account record with this person_id.

How can I perform this using Mybatis.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: One to One Inserts

Anand Joshi
I am using Annotations and I need to perform these inserts in bulk so will be using Batch

On Wednesday, March 21, 2018 at 5:22:50 PM UTC-4, Anand Joshi wrote:
I have 2 tables Account and Person with one to one relationship. Account has user login credentials and Person has more like Profile Information like name dob etc.
Account.id and Person.id are autogenerated values. Person.id is FK into Account.person_id.
So when I get a request I need to insert Person record first, obtain Person.id then insert Account record with this person_id.

How can I perform this using Mybatis.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: One to One Inserts

Guy Rouillier-2
In reply to this post by Anand Joshi
You don't mention which DBMS you are using.  Both Oracle and PostgreSQL provide an optional RETURNING clause on INSERT (and UPDATE and DELETE) that you can use to obtain the value of an autoincrement primary key column.  You can capture that value in MyBatis by wrapping the insert statement in a BEGIN-END block and making the statement CALLABLE.

Another option is useGeneratedKeys, which your JDBC driver must support.  Looks like both Oracle and PostgreSQL support that as well.

Finally, if your DBMS doesn't allow either of those, you can use the <selectKey> clause.  This option doesn't require support in the JDBC driver, but the downside is that it makes an extra round trip to the database on each insert.

--
Guy Rouillier

------ Original Message ------
From: "Anand Joshi" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 3/21/2018 5:22:50 PM
Subject: One to One Inserts

I have 2 tables Account and Person with one to one relationship. Account has user login credentials and Person has more like Profile Information like name dob etc.
Account.id and Person.id are autogenerated values. Person.id is FK into Account.person_id.
So when I get a request I need to insert Person record first, obtain Person.id then insert Account record with this person_id.

How can I perform this using Mybatis.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.