- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试将所有数据从一个数据库插入到另一个数据库。这两个数据库具有相同的结构(它们使用相同的实体)。我使用 PostgreSQL 9.1、Glassfish 4.0、EclipseLink (JPA 2.1) 和 Java EE 7 Web。
这些是实体:
@Entity
public class Store extends BaseEntity {
@NotNull
private String name;
/*
* required
*/
@OneToMany(cascade = CascadeType.PERSIST)
private List<Price> listPrices;
/*
* required
*/
@OneToMany(cascade = CascadeType.PERSIST)
private List<BusinessHours> listBusinessHours;
@OneToOne(cascade = CascadeType.PERSIST, optional = false)
private PointCoordinates pointCoordinates;
...
}
例如@OneToMany 注释实体之一:
@Entity
public class BusinessHours extends BaseEntity {
private Boolean holiday;
...
}
BaseEntity 包含带有 GenerationType.SEQUENCE 的 Id 和 serialVersionUID。
持久性.xml:
<persistence-unit name="restDBFromRawData" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/RestDBFromRawData</jta-data-source>
<class>info.mycompany.entities.BusinessHours</class>
<class>info.mycompany.entities.Store</class>
<class>info.mycompany.entities.PointCoordinates</class>
<class>info.mycompany.entities.Price</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
<persistence-unit name="restClientDB" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/RestClientDB</jta-data-source>
<class>info.mycompany.entities.BusinessHours</class>
<class>info.mycompany.entities.Store</class>
<class>info.mycompany.entities.PointCoordinates</class>
<class>info.mycompany.entities.Price</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
如果我尝试从一个数据库获取所有数据并尝试将它们插入另一个数据库 (addStoresToRestClientDB):
@Stateless
public class StoreOutputService {
@EJB
StoreOutputDAO storeOutputDAO;
@EJB
StoreOutputRestClientDAO storeOutputRCDAO;
public List<Store> getAllStores() {
return storeOutputDAO.findAll();
}
public void addStoresToRestDBFromRawData() {
//this works fine
...
for (Store store : listStore) {
storeOutputDAO.edit(store);
}
}
public void addStoresToRestClientDB() {
List<Store> listStore = getAllStores();
//size of list is right, the same like I insert in "addStoresToRestDBFromRawData"
for (Store store : listStore) {
storeOutputRCDAO.edit(store);
}
}
}
我收到以下错误:
WARNING: Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: insert or update on table "store_businesshours" violates foreign key constraint "fk_store_businesshours_listbusinesshours_id"
Detail: Key (listbusinesshours_id)=(55) is not present in table "businesshours".
Error Code: 0
Call: INSERT INTO STORE_BUSINESSHOURS (listBusinessHours_ID, Store_ID) VALUES (?, ?)
bind => [2 parameters bound]
Query: DataModifyQuery(name="listBusinessHours" sql="INSERT INTO STORE_BUSINESSHOURS (listBusinessHours_ID, Store_ID) VALUES (?, ?)")
...
Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table "store_businesshours" violates foreign key constraint "fk_store_businesshours_listbusinesshours_id"
Detail: Key (listbusinesshours_id)=(55) is not present in table "businesshours".
...
WARNING: StandardWrapperValve[info.mycompany.web.ApplicationConfig]: Servlet.service() for servlet info.mycompany.web.ApplicationConfig threw exception
org.postgresql.util.PSQLException: ERROR: insert or update on table "store_businesshours" violates foreign key constraint "fk_store_businesshours_listbusinesshours_id"
Detail: Key (listbusinesshours_id)=(55) is not present in table "businesshours".
DAO 应该没问题。 StoreOutputDAO
使用持久化单元 restDBFromRawData
而 StoreOutputRestClientDAO
使用持久化单元 restClientDB
。
JDBC jdbc/RestDBFromRawData
使用连接池与 javax.sql.ConnectionPoolDataSource
ressourcetype (classname: org.postgresql.ds.PGConnectionPoolDataSource) 和其他 JDBC "jdbc/RestClientDB”使用“javax.sql.XADataSource”资源类型(类名:org.postgresql.xa.PGXADataSource)。如果我在“jdbc/RestClientDB”中采用与在“jdbc/RestDBFromRawData”中相同的配置,我会收到以下错误消息:“本地事务已经有 1 个非 XA 资源”。
$GLASSFISH_HOME/glassfish/lib
和 $GLASSFISH_HOME/domains/domain1/lib
包含 PostgreSQL 驱动程序“postgresql-9.1-901.jdbc4.jar”。
如果我在数据库中使用 pgAdmin 查看,一切看起来都很好。它们具有相同的结构,其他一切正常:将数据插入另一个数据库并使用 StoreOutputDAO
插入数据,但 StoreOutputRestClientDAO
除外。
更新基础实体:
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
最佳答案
错误发生是因为其他数据库中不存在营业时间。确保插入它。
你在上面调用persist,它的Id是怎么定义的?将其迁移到其他数据库时,您可能需要清空其 Id 和版本。
关于java - PSQL异常 : ERROR: insert or update on table "*" violates foreign key constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18593633/
我的数据库有 很多 table (比如 400+),我只记得我要找的那个人的名字的一部分。 我知道 \d将显示所有表格,但这太多了。是否有一些命令可以列出名称与给定正则表达式匹配的所有表? 谢谢 最佳
有没有办法使用 psql 命令行界面显示表中的所有内容? 我可以使用\list显示所有数据库,\d显示所有表格,但如何显示表格中的所有数据? 最佳答案 较新版本:(从 8.4 - mentioned
我有一个奇怪的情况,which psql 结果是 /usr/local/php5/bin/psql。 我无法退出它,而且它是一个可执行文件。我在网上阅读的所有内容都不适用。 我试过这样做:pg_ctl
刚刚创建了一个登录角色,随后创建了一个由登录角色拥有的数据库(两者都具有相同的名称“。我指定了一个密码。键入 psql fplanner 并立即被带到没有它的控制台从密码询问。我登录到我的计算机的管理
使用 psql,我想格式化 bool 值以显示为 TRUE 和 FALSE 而不是 t和 f 默认情况下,因为我发现后者很难相互区分。 我可以通过在我的 ~/.psqlrc 中设置以下内容来更改 nu
我正在构建一个从 friend 那里克隆的 Rails 应用程序。我正在使用 Ubuntu 13.10,rails 3.2.14。我正在使用 postgresql 数据库,当我尝试运行 rake db
我对 psql 完全陌生,对某些术语不是特别熟悉。我正在按照此处链接中有关模仿的 ETL 流程的说明进行操作:https://github.com/chichukw/mimic-omop/blob/m
我正在尝试学习如何将 prisma 与 psql 数据库一起使用。 我在使用 id 是 uuid 字符串的引用时遇到问题。 我有一个用户模型: model User { id Str
lecture3-# SELECT * FROM flights; ERROR: syntax error at or near "psql" LINE 1: psql 这是代码和错误。我在 Env
我正在尝试执行两个 sql 命令(创建一个新的架构和表),如果执行失败,它将启用两个命令的回滚。我要连接的数据库是 AWS Redshift。 create schema if not exists
我正在尝试将新终端 Postgres shell (psql) 添加到 Windows 终端。我想将其添加到设置中的配置文件列表中。 这是 Windows 终端。 在设置文件中,我们可以添加配置文件列
我在使用 PostgreSQL 时遇到问题。我最近在 ubuntu 20.04 中安装了这个版本 (13+223.pgdg20.04+1) 的 postgresql 包。 我正在尝试运行 psql命令
我将我的 postgres 版本从 9.2.24 切换到 9.6,因为我需要 jsonb 兼容性以及其他最新功能。我在 centos 7 上运行虚拟机。 我决定删除所有现有的 postgres 实例(
大家好,正如标题所说,我在尝试从转储文件导入“创建”新数据库时遇到问题。当我尝试运行 sql 查询时 - 我收到有关 的错误 ' COPY ' .当你通过 psql 控制台运行时,我得到 wrong
每当我尝试以用户 postgres 身份运行 psql 命令时,我都会收到此错误。 psql: 无法连接到服务器: 没有那个文件或目录 服务器是否在本地运行并接受 Unix 域套接字“/var/run
heroku pg:psql 命令在尝试连接到数据库时抛出错误。 (venv) PS C:\Users\Mevin\Desktop\poster> heroku pg:psql ---> Connec
我是 Postgres 和 Bash 的新手,所以我不确定有什么区别。 我正在尝试在 bash 脚本中自动更新 Postgres 中的表。我有 .sql 文件并创建了 .pgpass 600 的文件。
我正在使用 Windows 10 并为其安装了 Psql (9.6.4)。安装时,它要求我输入 super 用户“postgres”的密码,我输入的是“asdfasdf”。所以安装后,我用 psql
更新:有人在下面提到这是重复的:https://dba.stackexchange.com/questions/269473/what-does-psql-usr-pgsql-11-lib-libpq
在 postgres.app 安装和运行时出现问题,但是当我输入 哪个psql然后我收到“/usr/bin/psql”而不是 postgres.app 期望的正确路径。当我回应我的 PATH 时,我得
我是一名优秀的程序员,十分优秀!