![](/img/trans.png)
[英]Hibernate OneToMany integrity constraint violation: foreign key no parent
[英]SQL Error : integrity constraint violation : foreign key no parent
我正在努力找出我的查詢做錯了什么。 我沒有創建數據庫,但這是結構:
3張桌子:
車輛:品牌、電機、價格、名稱、ID
選項:ID、描述、價格
vehicule_option:id_vehicule,id_option
我猜 vehicule_option 有 2 個外鍵,但我找不到我添加的問題。 這是我的第一部分工作正常的代碼:
public boolean create(Vehicule v){
String query = "INSERT INTO vehicule (MARQUE, MOTEUR, PRIX, NOM) VALUES (";
query += v.getMarque().getId() + ", "
+ v.getMoteur().getId() + ", "
+ v.getPrix() + ", \'"
+ v.getNom() + "\');";
for (Option o : v.getOptions()){
query += "INSERT INTO vehicule_option (id_vehicule, id_option) VALUES ("
+ v.getId() + ", " + o.getId() + ");";
}
try{
Statement state = this.connect.createStatement();
ResultSet rs = state.executeQuery(query);
} catch (SQLException e){
e.printStackTrace();
}
return true;
}
所以,我在這里所做的基本上是將兩個值插入到正確的列中。 它給了我以下異常:
java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no parent; SYS_FK_10132 table: VEHICULE_OPTION
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
at fr.ocr.sql.DAOVehicule.create(DAOVehicule.java:35)
at fr.ocr.ihm.AddCarDialogBox$1.actionPerformed(AddCarDialogBox.java:151)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.hsqldb.HsqlException: integrity constraint violation: foreign key no parent; SYS_FK_10132 table: VEHICULE_OPTION
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.Constraint.getException(Unknown Source)
at org.hsqldb.Constraint.checkInsert(Unknown Source)
at org.hsqldb.StatementDML.performIntegrityChecks(Unknown Source)
at org.hsqldb.StatementDML.insertSingleRow(Unknown Source)
at org.hsqldb.StatementInsert.getResult(Unknown Source)
at org.hsqldb.StatementDMQL.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 40 more
從您在此處顯示的內容來看,車輛選項似乎有兩個外鍵,分別是車輛表中的 id 和選項表中的 id。
當您插入時,車輛 id 不在車輛表中,或者選項 id 不在其選項表中。
您剛剛插入了一個新的vehicule
記錄,那么您如何知道v.getId()
中該記錄的 id 呢?
你沒有,而且v.getId()
很可能是0
,因為你正在創建一個新的。
如果您通過“o”變量進行迭代,您應該使用 o.getId() 而不是 v.getId()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.