質問

We have a many to many relation with let's say Entity1 and Entity2.

Now when we delete an element of Entity2, we don't in fact remove it from database but simply have a field "isActive" which we set to false.

When that happens, we would like however to remove all relations between any element of Entity1 to the element of Entity2 which was "deleted".

In SQL this transforms to a entity1 table, entity2 table, and entity1TOentity2 table. In SQL we can simply do a single update,

delete from entity1TOentity2 where entity2ID = :entity2Id

In JPA, we could load all elements of Entity1 as objects, with a join fetch on the relation, then remove one by one in Java the connections, but it's terribly wasteful...

Is there any way to formulate that SQL query with JPAQL?

I know it's possible to explicitly define a link entity, and make JPA use it, then I guess I could run a query basically identical to the SQL one on that link entity, but it seems a bit overkill.

am I missing a solution as nice as the raw SQL, but using the JPA API/JPAQL?

役に立ちましたか?

解決

Given your context, I see only one option: native queries.

他のヒント

On pure JPA, I don't know a clean way to do it.
If you're using Hibernate behind the scenes, there is a high-level solution:

@Entity
@Where(clause='isActive=true')
public class Entity2{
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top