我正在尝试为我的应用添加愿望清单功能,但我一直收到此错误:
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类:
@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
,SEQUENCE
,TABLE
,但都失败了。我还有一个名为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>
答案 0 :(得分:0)
最后它现在正在运作,
首先,我必须删除我从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的所有帮助,我从你的帮助中学到了很多。
答案 1 :(得分:0)
这可能是"Non nullable attributes" in JPA single-table-inheritance problem。指定
可能有所帮助,也没有什么坏处@JoinTable(name = "[some join table name]",
joinColumns = {@JoinColumn(name = "[some ID column name]")},
inverseJoinColumns = {@JoinColumn(name = "[some different ID column name]")})