There is way for an entity to belong to a user in Hibernate

Asked

Viewed 31 times

1

I am looking for a simplified way to link a bank entity to a specific user, who registered, in such a way that it does not need to set the field at the time of registration. Is there any simplified way to do this?

The biggest problem of placing a foreign key is that then you need to rewrite the querys so that the query takes into account the logged in user.

Example:

@Query(value = "SELECT * FROM produto where p.userid = ?1",
            countQuery = "SELECT count(*) FROM produto")
Page<Produto> findAll(long userid, Pageable pageable);

Which makes it difficult to function because for every new entity you create you need to rewrite it.

  • Simpler way than creating a non-null FK in such an entity representing the user? I don’t think you have.

  • The big X of the question in this case is that when we do a query we need to rewrite the query query in such a way that check if the id of the user logged in with jwt token is the same as the user who registered.

  • Look what I edited and put up.

  • I understand. I just don’t see the right solution to your problem. You can try a double primary key, but that would take parts out of my knowledge. Or a code key that combines a sequential and the user, and you would mount that key in the program.

  • But the ideal would be to assign a foreign key even. There is no point in not doing so. It’s the same idea of cutting the iron off the fence while it’s unlocked.

  • Another possible solution is to see if it is feasible to create an overload in the Hibernate interpreter, which would be verified by the user, whatever user you have in mind. Ai, your repositories would not be in charge of knowing which user it is, but you would have to do something about it.

  • There is no way to perform miracles, if these miracles are not with your information in the bank tables, (in).

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.