如何解决Java JPA 无法添加或更新子行:外键约束失败
我正在尝试创建一个包含团队列表的表格。这是季节表。但是,当我运行该程序时,出现以下错误:
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table team add constraint FKsrji3x3snoiiq1s2xei5lon2w foreign key (type_id) references season (season_id)]
at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applysqlString(SchemaMigratorImpl.java:431) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applysqlStrings(SchemaMigratorImpl.java:420) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applyForeignKeys(SchemaMigratorImpl.java:386) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:214) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:134) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:101) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:472) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
... 22 common frames omitted
Caused by: java.sql.sqlIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`eb322`.`#sql-3140_69e`,CONSTRAINT `FKsrji3x3snoiiq1s2xei5lon2w` FOREIGN KEY (`type_id`) REFERENCES `season` (`season_id`))
at com.MysqL.cj.jdbc.exceptions.sqlError.createsqlException(sqlError.java:115) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.MysqL.cj.jdbc.exceptions.sqlError.createsqlException(sqlError.java:95) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.MysqL.cj.jdbc.exceptions.sqlExceptionsMapping.translateException(sqlExceptionsMapping.java:122) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.MysqL.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1393) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.MysqL.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2353) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at com.MysqL.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1303) ~[mysql-connector-java-8.0.11.jar:8.0.11]
at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
... 32 common frames omitted
从我在这里看到的内容来看,它是关于添加数据的,但我不是在尝试添加数据,我只是在尝试创建当前的表!以下是相关类: 四季.java
@Entity
public class Season {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int season_id;
private String title;
@OnetoMany(mappedBy="season",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private List<Team> teams;
团队.java
@Entity
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "team_id")
private int Team_Id;
private String teamName;
private Blob logo;
private String stadium;
@OnetoOne
private TotalTeamStats tts;
@ManyToOne
@JoinColumn(name="TYPE_ID",nullable=false)
private Season season;
请谁能找出此约束失败的原因。我可以在 MysqLDatabase 中提供 sql 或 sql 图片,我真的需要它来为我的第三年项目工作! 谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。