简体   繁体   English

如何使用休眠管理具有多个数据库角色(用户)的 web 应用程序?

[英]How to manage a webapp with many DB roles (users) with hibernate?

How to handle the situation where we have a Postgres database running with many database roles (representing the users) and want to use hibernate so every database statement would be executed using a connection fetched with the specific user?如何处理我们有一个 Postgres 数据库运行多个数据库角色(代表用户)的情况,并希望使用 hibernate 以便使用与特定用户获取的连接来执行每个数据库语句?

To get a Session / EntityManager we need to fetch it from an EntityManagerFactory , which requires a DB user/password, usually specified in persistence.xml like this:要获取Session / EntityManager我们需要从EntityManagerFactory获取它,这需要数据库用户/密码,通常在persistence.xml指定,如下所示:

<property name="javax.persistence.jdbc.user" value="SYSDBA"/>
<property name="javax.persistence.jdbc.password" value="masterkey"/>

Of course i can create a Session / EntityManager for every user using a separate EntityManagerFactory , but this is a costly operation.当然,我可以创建一个Session / EntityManager使用一个单独的每个用户EntityManagerFactory ,但是这是一个代价高昂的操作。 How can this problem be solved?如何解决这个问题?

If the RDBMS is PostgreSQL I think the best way to accomplish this would be to call the SET ROLE command.如果 RDBMS 是 PostgreSQL,我认为最好的方法是调用SET ROLE命令。 This command will change the role and permissions to whatever role is specified.此命令会将角色和权限更改为指定的任何角色。 It will carry out all SQL commands during the session as if you logged in with that role in the beginning.它将在会话期间执行所有 SQL 命令,就像您在开始时以该角色登录一样。

Here is a link to the Postgres documentation. 这是 Postgres 文档的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate 4:如何动态管理数据库中的表 - Hibernate 4: How to dynamically manage tables in a db 如何从 Java 管理 S/4HANA 的用户和角色? - How to manage users and roles for S/4HANA from Java? 获取Java webapp中的所有用户和角色 - Get all users and roles in Java webapp 如何使用Hibernate管理两个表中的多对多关系 - How to Manage a Many-to-Many Relationship in Two Table using Hibernate 如何在Spring中使用休眠条件按角色分配用户 - How to get users by their roles using hibernate criteria in Spring 如何在休眠状态下管理多个一对多关系 - how to manage multiple one-to-many relationships in hibernate Hibernate如何在从数据库中保存/获取对象时管理序列化ID - how hibernate manage serialization id while saving/fetching object in/from DB 如何管理时间戳列的基于注释的Hibernate版本控制,其中DB生成值 - How to manage annotation based Hibernate versioning for time stamp column in which value generated by DB 如何使用Hibernate多对多方法将jsp页面中多个select标记中的数据保存到DB - How to save data from multiple select tag in jsp page to DB using Hibernate many-to-many Spring Security DB UserDetailsS​​ervice不考虑用户的角色 - Spring security db UserDetailsService doesn't take in account the roles of users
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM