2013-11-05 107 views
0

我一直在看這個整個下午,我很難過。爲什麼EclipseLink使用重複值?它怎麼樣?EclipseLink生成重複主鍵

這個實體類使用Hibernate很好,但我試圖讓應用程序在Glassfish 3.1.2.2下運行。

應用程序失敗:

[#|2013-11-04T15:54:17.190-0700|WARNING|glassfish3.1.2|org.eclipse.persistence.session.file:/C:/glassfish3/glassfish/domains/domain1/applications/foo_jar/_bar.sql|_ThreadID=143;_ThreadName=Thread-2;|Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException 
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (GAPM.SYS_C0033927) violated 

Error Code: 1 
Call: INSERT INTO TIME_DIMENSION (ID, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR, HOUR, JWK_WEEK, JWK_YEAR, MINUTE, MONTH, MONTH_NAME, QUARTER, WEEKDAY, YEAR) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
bind => [3565649, 4, 1, 308, 19, 45, 2013, 7, 11, November, 2, Monday, 2013] 
Query: InsertObjectQuery([email protected]) 
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324) 
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:840) 
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:906) 

當我用grep在服務器日誌中的主鍵值,我發現:

[#|2013-11-04T18:02:18.846-0700|FINE|glassfish3.1.2|org.eclipse.persistence.session.file:/C:/glassfish3/glassfish/domains/domain1/applications/foo_jar/_bar.sql|_ThreadID=158;_ThreadName=Thread-2;ClassName=null;MethodName=null;|INSERT INTO TIME_DIMENSION (ID, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR, HOUR, JWK_WEEK, JWK_YEAR, MINUTE, MONTH, MONTH_NAME, QUARTER, WEEKDAY, YEAR) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
    bind => [3069178, 2, 6, 306, 15, 44, 2013, 55, 11, November, 2, Saturday, 2013]|#] 
-- 
[#|2013-11-04T18:02:18.865-0700|FINE|glassfish3.1.2|org.eclipse.persistence.session.file:/C:/glassfish3/glassfish/domains/domain1/applications/foo_jar/_bar.sql|_ThreadID=158;_ThreadName=Thread-2;ClassName=null;MethodName=null;|INSERT INTO TIME_DIMENSION (ID, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR, HOUR, JWK_WEEK, JWK_YEAR, MINUTE, MONTH, MONTH_NAME, QUARTER, WEEKDAY, YEAR) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
    bind => [3069178, 3, 7, 307, 5, 44, 2013, 37, 11, November, 2, Sunday, 2013]|#] 
-- 

的JPA註解實體如下:

@Entity 
@Table(name="TIME_DIMENSION") 
public class TimeDimension implements Dimension { 

private static final long serialVersionUID = -7496979683190776675L; 

    @Id 
    @Column(name="ID") 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TIME_DIM_SEQ") 
    @SequenceGenerator(name="TIME_DIM_SEQ",sequenceName="TIME_DIM_SEQ") 
    private long id; 

    ... 

該數據庫是Oracle 11g。我沒有做任何事情告訴EclipseLink底層數據庫是Oracle。我需要嗎?我試圖用 不同的選項重新創建索引,是的,增量是1.任何想法?

+0

你能嘗試添加'allocationSize = 1'?如下所示:'@SequenceGenerator(name =「TIME_DIM_SEQ」,sequenceName =「TIME_DIM_SEQ」,allocationSize = 1)'這裏寫的是您必須提供'allocationSize'等於序列的'INCREMENT BY':[EclipseLink - PrimaryKey](http://wiki.eclipse.org/EclipseLink/Examples/JPA/PrimaryKey) –

+0

引發異常的對象是如何保持的?這是一個可能持續存在的現有對象嗎?如果排序有問題,它會頻繁發生插入,因此這種情況發生的頻率以及在什麼情況下會發生?什麼是persistence.xml的樣子? – Chris

+0

@Przemyslaw如果你想把它放在答案中,我會給你複選標記。關鍵在於,EclipseLink在未指定時將defaultSize分配爲50。儘管我理解這種方法的性能優勢,但我從不會猜測默認值不是1. – sceaj

回答

1

您能否嘗試加入allocationSize=1?如:

@SequenceGenerator(name="TIME_DIM_SEQ",sequenceName="TIME_DIM_SEQ",allocationSi‌​ze=1) 

它寫在這裏,你必須提供allocationSize等於序列的INCREMENT BY

EclipseLink - Primary Key

+0

正如我在我以前的評論中所提到的,省略時,allocationSize的默認值是50,而不是更直觀的(在我看來)值1。 – sceaj