Java 如何解决SQL完整性约束违规异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22528413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:17:10  来源:igfitidea点击:

how to Solve SQL Integrity Constraint Violation Exception

javasqlpersistence.xml

提问by mussdroid

I created an entity class that has the same properties as project.java, and created a class where I can persist the entity object. Also, I created a database using databases in Netbeans using embedded JDBC. I have the persistence.xml, which provides the properties to connect the db, and is used the persitence class on entitymanagerfactoryobject. The connection seems fine but I am having Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'PROJECT_ID' cannot accept a NULL value.error.

我创建了一个与 project.java 具有相同属性的实体类,并创建了一个可以持久化实体对象的类。此外,我使用嵌入式JDBC. 我有persistence.xml,它提供连接数据库的属性,并在entitymanagerfactory对象上使用持久性类。连接看起来很好,但我遇到了Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'PROJECT_ID' cannot accept a NULL value.错误。

Is it ok to create the db manually (executing the ddl) or should I create the table in the persistence.xmlusing property name="javax.persistence.jdbc.url"" value="create-tables?

可以手动创建数据库(执行 ddl)还是应该在persistence.xmlusing 中创建表property name="javax.persistence.jdbc.url"" value="create-tables

Regards

问候

回答by mursch

You have to set the value of the column PROJECT_ID.

您必须设置 column 的值PROJECT_ID

You can either do this in your code, for example by using the annotations @SequenceGeneratoror @GenericGenerator, or use db features (trigger) to set the id during insert.

您可以在代码中执行此操作,例如通过使用注释@SequenceGenerator@GenericGenerator,或使用 db 功能(触发器)在插入期间设置 id。