gpt4 book ai didi

java - 不能接受 @ID @generatedvalue JPA 实体上的 NULL 值

转载 作者:行者123 更新时间:2023-11-29 08:57:08 26 4
gpt4 key购买 nike

我正在尝试为我的应用添加一个添加到愿望 list 的功能,但我一直收到此错误:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):           org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'WISH_ID' cannot accept a NULL value.
Error Code: -1
Call: INSERT INTO WISHLIST (BOOK_TITLE, CUSTOMER_ID) VALUES (?, ?)
bind => [2 parameters bound]
Query: InsertObjectQuery(dukesbookstore.entity.Wishlist[ wishId=null ])

实体类:

@Entity
@Table(name = "WISHLIST")
@XmlRootElement
@NamedQueries(
{
@NamedQuery(name = "Wishlist.findByCustomerId", query = "SELECT w FROM Wishlist w WHERE w.customerId = :customerId"),

})
public class Wishlist implements Serializable
{
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "WISH_ID")
private Integer wishId;
@Column(name = "CUSTOMER_ID")
private Integer customerId;
@Size(max = 35)
@Column(name = "BOOK_TITLE")
private String bookTitle;

public Wishlist()
{
}



public Integer getCustomerId()
{
return customerId;
}

public void setCustomerId(Integer customerId)
{
this.customerId = customerId;
}

public String getBookTitle()
{
return bookTitle;
}

public void setBookTitle(String bookTitle)
{
this.bookTitle = bookTitle;
}
}

这是创建新愿望的代码:

public void createWishlist(String title,int cust_id)
{
Wishlist newWish = new Wishlist();
newWish.setBookTitle(title);
newWish.setCustomerId(cust_id);
em.persist(newWish);
}

我试图查看其他类似的问题,但它们涉及我没有使用的 hibernate 。我也尝试过各种生成策略,例如 AUTOSEQUENCETABLE 但都失败了。我还有另一个名为 customer 的实体,它完全相同,但尽管它是从表单创建的,但它工作正常。

更改为 AUTO 会产生此错误:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):   org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'SEQUENCE' does not exist.
Error Code: -1
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
bind => [2 parameters bound]
Query: DataModifyQuery(name="SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?")
root cause

java.sql.SQLSyntaxErrorException: Table/View 'SEQUENCE' does not exist.
root cause

org.apache.derby.client.am.SqlException: Table/View 'SEQUENCE' does not exist.

Persistence.xml 包含相关内容

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="myStorePU" transaction-type="JTA">
<jta-data-source>mydb</jta-data-source>
<class>myStore.entity.Book</class>
<class>myStore.entity.Customer</class>
<class>myStore.entity.Wishlist</class>

<properties>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>

<property name="eclipselink.logging.level" value="FINE"/>
</properties>

</persistence-unit>

最佳答案

终于可以用了

首先,我必须删除我从 netbeans 数据库创建工具创建的表,它具有这样的创建代码,如使用“抓取结构”所见

create table "APP".WISHLIST
(
WISH_ID NUMERIC(5) not null primary key,
CUSTOMER_ID NUMERIC(5),
BOOK_TITLE VARCHAR(100)
)

其次,我将这段代码添加到我的 persistence.xml 文件中

<property name="eclipselink.ddl-generation" value="create-tables"/>

这解决了问题,因为它自己创建了表,它们具有不同的创建代码,从自动创建的抓取结构可以看出:

create table "APP".WISHLIST
(
WISH_ID INTEGER default GENERATED_BY_DEFAULT not null primary key,
CUSTOMER_ID NUMERIC(5),
BOOK_TITLE VARCHAR(100)
)

所以,基本上应该让 netbeans 从实体创建表本身,但我使用的是“从表创建实体”功能,为此我必须先在 netbeans gui 中创建表。

感谢@Geziefer 的所有帮助,我也从你的帮助中学到了很多东西。

关于java - 不能接受 @ID @generatedvalue JPA 实体上的 NULL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19649896/

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