macOS 中的 Postgres 和 JPA
Postgres and JPA in macOS
我有一个带有 Postgres 9.6 和 Wildfly 14 的 windows 环境。我已经通过 wildfly 创建了如下连接:
<datasource jndi-name="java:jboss/datasources/mydb" pool-name="mydb" enabled="true">
<connection-url>jdbc:postgresql://localhost:5432/mydb</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
<driver>postgresql-42.1.1.jar</driver>
<security>
<user-name>someusername</user-name>
<password>somepassword</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
</datasource>
然后我的 Persistence.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="SomePU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/mydb</jta-data-source>
</persistence-unit>
</persistence>
我的 JPA 实体如下所示:
@Entity @Table(name="usr_apps")
public class SomeApp{...}
我的 EJB 无状态 class:
@PersistenceContext(unitName = "SomePU")
private EntityManager em;
public List getSometing(){
return em.createQuery("select b from SomeApp b").getResultList();
}
相同的配置在 Linux Ubuntu、Linux RHEL、Mac OSX Lion 上运行。
但现在我正尝试在 macOS mojave 上 运行 它,我在尝试通过 JPA 读取数据时收到此错误。
[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) ERROR: relation "usr_apps" does not exist
Position: 150
我很确定 table 存在。我可以在任何其他 DBMS 中看到相同的查询 运行ning。我已从我的其他环境中转储数据库并将其恢复到新环境中。
PS。我也试过 PSQL 10。同样的结果。没有数据正在从数据库中读取。我可以从 Wildfly 管理控制台成功 ping 连接。
我还确保 table 在 public 架构中。
编辑:
我意识到即使连接设置为 mydb,但 JPA 仅将查询发送到 postgres 默认数据库!
编辑 2
好的,这对我来说越来越接近错误了。
<datasource jndi-name="java:jboss/datasources/mydb" pool-name="mydb">
<connection-url>jdbc:postgresql://localhost:5432/mydb</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
<driver>postgresql-42.2.5.jar</driver>
<security>
<user-name>testuser</user-name>
<password>testpassword</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
</datasource>
我收到这个错误:
Caused by: org.postgresql.util.PSQLException: FATAL: database "testuser" does not exist
为什么 JPA 将我的用户名视为数据库名称?
PS。野飞 14.0.1.Final
好的!!我所要做的就是从连接中删除数据源 class :
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
终于成功了
请注意,当您通过管理控制台创建数据源时,wildfly 版本 14 及更高版本会自动将此行添加到 xml 文件中。
我有一个带有 Postgres 9.6 和 Wildfly 14 的 windows 环境。我已经通过 wildfly 创建了如下连接:
<datasource jndi-name="java:jboss/datasources/mydb" pool-name="mydb" enabled="true">
<connection-url>jdbc:postgresql://localhost:5432/mydb</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
<driver>postgresql-42.1.1.jar</driver>
<security>
<user-name>someusername</user-name>
<password>somepassword</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
</datasource>
然后我的 Persistence.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="SomePU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/mydb</jta-data-source>
</persistence-unit>
</persistence>
我的 JPA 实体如下所示:
@Entity @Table(name="usr_apps")
public class SomeApp{...}
我的 EJB 无状态 class:
@PersistenceContext(unitName = "SomePU")
private EntityManager em;
public List getSometing(){
return em.createQuery("select b from SomeApp b").getResultList();
}
相同的配置在 Linux Ubuntu、Linux RHEL、Mac OSX Lion 上运行。
但现在我正尝试在 macOS mojave 上 运行 它,我在尝试通过 JPA 读取数据时收到此错误。
[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) ERROR: relation "usr_apps" does not exist
Position: 150
我很确定 table 存在。我可以在任何其他 DBMS 中看到相同的查询 运行ning。我已从我的其他环境中转储数据库并将其恢复到新环境中。
PS。我也试过 PSQL 10。同样的结果。没有数据正在从数据库中读取。我可以从 Wildfly 管理控制台成功 ping 连接。
我还确保 table 在 public 架构中。
编辑:
我意识到即使连接设置为 mydb,但 JPA 仅将查询发送到 postgres 默认数据库!
编辑 2
好的,这对我来说越来越接近错误了。
<datasource jndi-name="java:jboss/datasources/mydb" pool-name="mydb">
<connection-url>jdbc:postgresql://localhost:5432/mydb</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
<driver>postgresql-42.2.5.jar</driver>
<security>
<user-name>testuser</user-name>
<password>testpassword</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
</datasource>
我收到这个错误:
Caused by: org.postgresql.util.PSQLException: FATAL: database "testuser" does not exist
为什么 JPA 将我的用户名视为数据库名称?
PS。野飞 14.0.1.Final
好的!!我所要做的就是从连接中删除数据源 class :
<datasource-class>org.postgresql.ds.PGSimpleDataSource</datasource-class>
终于成功了
请注意,当您通过管理控制台创建数据源时,wildfly 版本 14 及更高版本会自动将此行添加到 xml 文件中。