gpt4 book ai didi

java - ArrayIndexOutOfBoundException - hsqldb?

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:41 24 4
gpt4 key购买 nike

我目前正在使用 hsqldb。我的创建语句如下所示:

CREATE TABLE Movie
(
movieId INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title VARCHAR(255) NOT NULL,
);

CREATE TABLE Playlist(
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR(255) NOT NULL,
);

CREATE TABLE PlaylistMovies(
mid INTEGER FOREIGN KEY REFERENCES Movie(movieid),
pid INTEGER FOREIGN KEY REFERENCES Playlist(id),
PRIMARY KEY (mid, pid)
);
//my assoziation table which safes the movie ids and playlist ids


alter table playlistmovies
add constraint fk_plm_playlist
foreign key (pid, mid) references playlist(id)
on delete cascade;

当我想做的时候:

INSERT INTO PlaylistMovies(MID, PID) VALUES (1,1);

我得到:

enter image description here

但是,数据存在于其他两个表中,因此应该可以引用?

这里有什么问题以及如何解决它?

更新:

我使用的版本:hsqldb-2.2.8

java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException: 1 java.lang.Arr
ayIndexOutOfBoundsException: 1
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
at org.hsqldb.util.DatabaseManagerSwing.executeSQL(Unknown Source)
at org.hsqldb.util.DatabaseManagerSwing.access$1000(Unknown Source)
at org.hsqldb.util.DatabaseManagerSwing$StatementExecRunnable.run(Unknow
n Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException: 1 java.lang.Arr
ayIndexOutOfBoundsException: 1
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
at org.hsqldb.util.DatabaseManagerSwing.executeSQL(Unknown Source)
at org.hsqldb.util.DatabaseManagerSwing.access$1000(Unknown Source)
at org.hsqldb.util.DatabaseManagerSwing$StatementExecRunnable.run(Unknow
n Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

更新2:

好的,伙计们,

我尝试了一点,现在我知道,当我不创建更改表时,我可以轻松插入字段......

最佳答案

该语句有错误,应被 HSQLDB 拒绝。

alter table playlistmovies
add constraint fk_plm_playlist
foreign key (pid, mid) references playlist(id) -- mistake, FK has two columns, PK has one column
on delete cascade;

它被接受,但之后,当您尝试插入值时,原始错误会导致异常(更新:此问题已在 HSQLDB 2.3.0 中修复,并且它拒绝原始语句)。

您不能在两列上使用 FK 来引用只有一列的 PK

可以将ON DELETE CASCADE添加到原来的CREATE TABLE

CREATE TABLE PlaylistMovies(
mid INTEGER FOREIGN KEY REFERENCES Movie(movieid) ON DELETE CASCADE,
pid INTEGER FOREIGN KEY REFERENCES Playlist(id) ON DELETE CASCADE,
PRIMARY KEY (mid, pid)
);

关于java - ArrayIndexOutOfBoundException - hsqldb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com