使用 JPA 获取数据库用户并在实体属性中设置

Get the database user using JPA and set in the Entity Attribute

我是 JPA 的新手。我在数据库中有这些 table,其中列 Created_By 不可为空。问题是我遇到了约束问题,因为此值不可为空。

数据库:Oracle 11G 应用服务器:IBM Websphere 使用 JNDI 作为数据源 事务 Table:

Customer_Id | Amount | Status  | Created_BY   | Created_Date  |
1           | 10    | Pending  |   (USER)     |  (new Date()) |

在我的 Java 代码中,我没有设置 CreatedBy 和属性,因为它们在我的数据库脚本中默认设置,其中日期是 SYSDATE。

Customers customer = new Customer();
customer.setCustomerId(12324);
customer.setAmount(10);
customer.setStatus(StatusType.PENDING.getName());
customer.setCreatedDate(new Date());

我是否可以使用 JPA 从数据库中访问用户?不使用本机查询 "Select user from Dual"。

我可以按照下面的代码获取连接用户名。

 EntityManagerFactoryInfo info = (EntityManagerFactoryInfo) em.getEntityManagerFactory();
 String userName = info.getPersistenceUnitInfo().getNonJtaDataSource().getConnection().getMetaData().getUserName();