的EclipseLink似乎是不正确地传递一个空主键值来德比持续与产生主键的表时。 德比返回尝试在这种情况下修改标识列的错误。 德比需要排除的id值的SQL语句。 我的问题是我怎么强迫的EclipseLink发出正确的SQL? 详细情况如下...
我使用的EclipseLink映射到最新的Netbeans 7.3beta2下Derby数据库(v10.8.1.2)。
该数据库表的主键生成自动:
CREATE TABLE STUDENT_BATCH (
ID bigint PRIMARY KEY
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
FILENAME varchar(200) NOT NULL,
SCHOOLBOARD varchar(100) NOT NULL,
CREATE_TS timestamp NOT NULL,
CONTACT_INFO varchar(200),
NOTES varchar(2000),
BOARD_NAME varchar(100)
)
;
相应的JPA类指定的ID使用的身份策略生成:
@Entity
@Table(name = "STUDENT_BATCH")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "StudentBatch.findAll", query = "SELECT s FROM StudentBatch s")})
public class StudentBatch implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Basic(optional = false)
@Column(name = "FILENAME")
private String filename;
@Basic(optional = false)
@Column(name = "SCHOOLBOARD")
private String schoolboard;
@Basic(optional = false)
@Column(name = "CREATE_TS")
@Temporal(TemporalType.TIMESTAMP)
private Date createTs;
@Column(name = "CONTACT_INFO")
private String contactInfo;
@Column(name = "NOTES")
private String notes;
@Column(name = "BOARD_NAME")
private String boardName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "studentBatchId")
private Collection<StudentRecord> studentRecordCollection;
methods etc follow...
当我去坚持类,我不指定ID,以便德比将提供ID。
StudentBatch sb = new StudentBatch();
sb.setBoardName(meta.get("BOARD NAME"));
sb.setContactInfo(meta.get("CONTACT INFO"));
sb.setCreateTs(new Date());
sb.setFilename(event.getFile().getFileName());
sb.setNotes(meta.get("NOTES"));
sb.setSchoolboard(meta.get("SCHOOL BOARD"));
_logger.debug("persisting batch");
em.persist(sb);
_logger.debug("flushing");
em.flush();
_logger.debug("flushed");
的EclipseLink,然而,经过ID德比为空,德比给了错误尝试修改标识列 :
INFO: DEBUG 11710 27 Nov 2012 18:17:10,558 [http-thread-pool-8080(4)] (FileUploadController.java:75) - persisting batch
INFO: DEBUG 11713 27 Nov 2012 18:17:10,561 [http-thread-pool-8080(4)] (FileUploadController.java:77) - flushing
WARNING: Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'ID'.
Error Code: -1
Call: INSERT INTO STUDENT_BATCH (ID, BOARD_NAME, CONTACT_INFO, CREATE_TS, FILENAME, NOTES, SCHOOLBOARD) VALUES (?, ?, ?, ?, ?, ?, ?)
bind => [7 parameters bound]
Query: InsertObjectQuery(ca.ontario.mhltc.studentupload.model.StudentBatch[ id=null ])
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
这有一定道理给我,因为如果我尝试用id字段上的SQL命令行执行插入指定我得到一个错误了。
INSERT INTO STUDENT_BATCH (id, BOARD_NAME, CONTACT_INFO, CREATE_TS, FILENAME, NOTES, SCHOOLBOARD)
VALUES (null, 'abc', 'def', current_timestamp, 'aaa', 'aabb', '1234');
使我这个:
Error code -1, SQL state 42Z23: Attempt to modify an identity column 'ID'.
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
然而,这条INSERT语句是成功的:
INSERT INTO STUDENT_BATCH (BOARD_NAME, CONTACT_INFO, CREATE_TS, FILENAME, NOTES, SCHOOLBOARD)
VALUES ('abc', 'def', current_timestamp, 'aaa', 'aabb', '1234');
回报
Executed successfully in 0.002 s, 1 rows affected.
Line 1, column 1
Execution finished after 0.002 s, 0 error(s) occurred.
在我看来,的EclipseLink应该知道德比是如何处理标识列,不应该在所有通过id列的INSERT语句。 是否有这样或我,我应该只转储德比和使用其他数据库中的解决方法吗?