JDBI how can I delete data from two tables in one #SqlUpdate - java

I have two tables:
entities:
- id
- someValues
and other table, with foregin key on id field
connectedEntities:
- entityId (foregin)
- otherObjectId (foregin)
I need to delete entity by id, but when I tried to delete just from entities table, there's FK violation.
Also, I want to use #SqlUpdate or other annotation from JDBI framework.
Does anybody know how to do something like this?

Thanks to #G_H , I found what I was looking for.
I just needed to add ON DELETE CASCADE to my foregin key definition, and everything worked just fine.
Here's the tutorial - mysqltutorial.org/mysql-on-delete-cascade

Related

Spring Boot JPA how to handle #Id if I use composite key

I am running a Spring Boot application were I use JPA. I am using Postgresql as DB.
I get some data from an api where there is no Id for each row. So I have to use #Id and #GeneratedValue. But my problem is that when I request this data again I might get the exact same rows or some of them again. I can make a composite key that consist of 3 of the columns but then how do I do with the Id that I would like to keep (autogenerated value). If I understand correct, I canĀ“t use composite primary key and a generate #Id at the same time?
An extension of this question is that sometimes a column in each row are updated. Then I would like to update only this column and not create a new row in the DB.
I could make a modified query that checks if the three column (composite key) already exist then I update that row if any change or if row is missing just save. Or is their any other option that I have missed so I do not have to write any query myself?
You are correct, you have to decide if you want to use a single autogenerated Id or a composite key, you can't have both at the same time. And if your API can't handle a single Id you are probably better of doing as you are thinking in your extended question; creating a composite key and when updating rows check for a row that match your three values.

Updating existing JPA child entities with FK in a single query

I have a parent/child one-to-many unidirectional relation.
Both the parent and the child POJOs use custom Insert-on-duplicate-update insert queries specified via #SQLInsert annotation.
Parent/child pair is inserted via cascading - everything works fine and the entity manager is closed.
Then I create a new entity manager and try to insert the same parent/child pair, but with updated values (same primary keys, but some of the other fields are changed).
The parent's Insert-on-duplicate-update works just fine and the values are updated.
In the child table however we end up with 2 entries, one with the old values and NULL parent_id foreign key, and one with the new values and correctly set FK.
I looked at the queries generated by Hibernate and found out the following:
The child row is inserted with the foreign key set to NULL
Then an update query is ran that sets the parent_id for the given child id
Another update is ran that sets the parent_id to NULL, I'm assuming for the child id of the first row
A third update query is ran that sets the parent_id
Is there a way for cascading insert to insert the child row and set the FK in the same query, thus eliminating the need for running a separate update query?
The question was answered as a part of a different question of mine:
JPA insert parent/child results in MySQLIntegrityConstraintViolationException
Basically, you have to set nullable = false in the #JoinColumn that specifies creates the parent/child relation.
There still seems to be an update query running for some reason but the complete record is being inserted with the insert statement.
The accepted answer did not solve my problem.
Adding updatable=false to the parent entity solved it. JPA does not execute the update query. However, I have no clue why that's the case and in fact, I don't think what I am doing is correct because it means I cannot update the child table later on if I have to.

How change hibernate association mapping from 'one to many' into 'many to many'?

The relation between the entities must be changed and I'd like to know is it normal to change the association mapping type and whether the data that already exist in db will be transfered normally? I tried to find information about it but didn't found. Or if the mapping will be changed the data that already exists must be transfered manually via sql queries? Thanks
If the association already uses a join table, and the mapping of the many-to-many keeps using the same join table with the same column names, you won't have to do anything except removing the unicity constraint you could have on one of the ci=olumns of the join table.
Otherwise, yes, obviously, you'll have to migrate your schema, using SQL, or any other tool (FlywayDB, Liquibase, etc.).
I could do it simply:
Let A and B be the original tables.
A->B (N-1) was moved to A<-AB->B (N-N).
I had to
- "remove" the foreignkey column from table A,
in favor of records to be inserted into AB
(made of the two foreign keys leading to A and B)
That's all.
Step one: Replace your Many-to-One annotation by your Many-to-Many annotations.
and lauch hibernate in append mode to generate the N-N table
Step two: Insert record in this N-N table given what is found in the remaining foreign-key column of you 1-N relationship.
Step three: Delete this foreign-key column.

How to delete a ManyToMany item using JPQL?

I have two models, say :
BlogPost(title, message)
Tags(name)
Both have a ManyToMany relationship defined.
Using JPQL, I delete a list of BlogPost with this query:
DELETE FROM BlogPost b WHERE b IN :list
(:list is a List from a previous SELECT requet).
Doing so, I have a ConstraintViolationException because of the relation between BlogPost and Tags.
Is there a way to delete the relation without deleting the Tags using JPQL?
Thanks for your help!
You have to remove the association first, before you delete the entity.
JPA create a table BlogPost_Tags which stores ID of BlogPost and Tags.
So when you try to delete a BlogPost, the constraint on the BlogPost_Tags failed.
You need to delete the relation before delete the Post, and there is no easy way in JPQL, you have to use the EntityManager.
I'll answer myself with the solution I came up. I'm not sure it's the best one, but at least, it works.
Since you want to bulk delete something that have a ManyToMany related items, you first have to delete the relation (in the join table), or do a loop and for each item, delete manually (insane and too much heavy).
So, since JPQL does not allow to do it, a possible way is to make a native SQL query for deleting the id you want in the related table, and then, do the bulk delete.

java hibernate eclipse xml - use an entity without a primary key

I have one question that if we are creating any table in database i.e. not having any primary key(all columns values are nullable).
can't we do mapping without ID field in Hibernate O/R Mapping i.e. .hbm.xml file while working with hibernate.
The problem which i am facing is my .hbm.xml file without ID field is not getting validated.
I got the answers from u all, but i have one question that can we use transient keyword prefixed to ID variable in the entity class so that not to persist that value into database?
The only real solution is to fix your broken data model. An entity that does not have a primary key is not relational data, so you can't expect an ORM product to be able to know how to handle it.
And how Hibernate will perform update without a primary key or an uniquely identified row?
Thanks.
You cannot do that in hibernate. And you shouldn't do it anyway. Databases warn you that it is not advisable not to have a primary key.
If you really don't have a set of columns that form a unique key, you have two options:
add an auto-increment primary key (preferred)
use raw JDBC to get the data from that particular table. You can do that with hibernate's session.doWork(..) method.

Categories