2

我正在尝试为我的应用添加愿望清单功能,但我不断收到此错误:

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);
  }

我试图查看其他类似的问题,但它们涉及我没有使用的休眠。我也尝试过各种生成策略,例如AUTO, SEQUENCETABLE但都失败了。我还有另一个名为 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>

4

2 回答 2

0

现在终于可以正常工作了

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

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 的所有帮助,我也从你的帮助中学到了很多东西。

于 2013-10-30T03:44:13.473 回答
0

这可能是JPA 单表继承问题中的“不可为空的属性”。指定它可能会有所帮助并且没有坏处

@JoinTable(name = "[some join table name]",
    joinColumns = {@JoinColumn(name = "[some ID column name]")},
    inverseJoinColumns = {@JoinColumn(name = "[some different ID column name]")})
于 2017-06-11T20:36:42.187 回答