我使用Eclipselink作为Postgres数据库的持久性提供程序的Java SE应用程序,我在应用程序初始化时遇到奇怪的行为。 事实是我的一个实体的ID被设置为从postgres汇集而来。序列。在初始化应用程序时,将日志记录级别设置为精细,我可以看到此ID被提取两次,因此序列每次增加1。 日志:
[EL Info]: 2015-01-22 19:25:13.659--ServerSession(1107730949)--Thread(Thread[main,5,main])--EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
[EL Fine]: connection: 2015-01-22 19:25:13.751--Thread(Thread[main,5,main])--Detected database platform: org.eclipse.persistence.platform.database.PostgreSQLPlatform
[EL Config]: connection: 2015-01-22 19:25:13.766--ServerSession(1107730949)--Connection(1370979551)--Thread(Thread[main,5,main])--connecting(DatabaseLogin(
platform=>PostgreSQLPlatform
user name=> "intime"
datasource URL=> "jdbc:postgresql://localhost:5432/InTime?charSet=LATIN2"
))
[EL Config]: connection: 2015-01-22 19:25:13.77--ServerSession(1107730949)--Connection(1142931081)--Thread(Thread[main,5,main])--Connected: jdbc:postgresql://localhost:5432/InTime?charSet=LATIN2
User: intime
Database: PostgreSQL Version: 9.3.5
Driver: PostgreSQL Native Driver Version: PostgreSQL 9.3 JDBC4.1 (build 1102)
[EL Info]: connection: 2015-01-22 19:25:13.901--ServerSession(1107730949)--Thread(Thread[main,5,main])--file:/home/tepo/IdeaProjects/WebParser/target/classes/_parser_writable login successful
[EL Fine]: sql: 2015-01-22 19:25:13.986--ServerSession(1107730949)--Connection(1142931081)--Thread(Thread[main,5,main])--select nextval(timetable)
[EL Fine]: sql: 2015-01-22 19:25:13.99--ServerSession(1107730949)--Connection(1142931081)--Thread(Thread[main,5,main])--ALTER SEQUENCE timetable INCREMENT BY 1
[EL Fine]: sql: 2015-01-22 19:25:13.992--ServerSession(1107730949)--Connection(1142931081)--Thread(Thread[main,5,main])--select nextval(timetable)
[EL Fine]: sql: 2015-01-22 19:25:13.993--ServerSession(1107730949)--Connection(1142931081)--Thread(Thread[main,5,main])--ALTER SEQUENCE timetable INCREMENT BY 1
[EL Fine]: sql: 2015-01-22 19:25:14.041--ServerSession(1107730949)--Connection(1142931081)--Thread(Thread[main,5,main])--SELECT ID, CODE, DAYCAT FROM "public"."DAYCATCODEMAP"
[EL Fine]: sql: 2015-01-22 19:25:14.049--ServerSession(1107730949)--Connection(1142931081)--Thread(Thread[main,5,main])--SELECT ID, CODE, READABLE FROM "public"."CT_EXCLUSION"
[EL Fine]: sql: 2015-01-22 19:25:14.05--ServerSession(1107730949)--Connection(1142931081)--Thread(Thread[main,5,main])--SELECT ID, ABBREVIATION, FULLNAME FROM "public"."REGION"
即使在初始化entityManagerFactory之前,这也会在应用初始化的最初阶段发生。
最后,当应用程序运行时,这会导致记录的转移带有移位的ID。 这是一个实体:
@Table(name = "\"TIMETABLE\"", catalog = "public")
@Entity
@NamedQuery(
name = "deleteTimetableByIds",
query = "DELETE FROM Timetable T where T.id = :id"
)
@SequenceGenerator(name="TTBL_SEQ", sequenceName="timetable", allocationSize = 1)
public class Timetable{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TTBL_SEQ")
private Long id;
...
}
的persistence.xml:
<persistence-unit name="parser_writable" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.target-database" value="PostgreSQL" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://localhost:5432/InTime?charSet=LATIN2"/>
<property name="javax.persistence.jdbc.user" value="intime"/>
<property name="javax.persistence.jdbc.password" value="intime"/>
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<property name="eclipselink.ddl-generation.output-mode"
value="database"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
所以,当我初始化一个暗示这个方法的PersistenceManager类时会发生这种情况:
private static EntityManager em_write = null;
private static EntityManagerFactory factory;
public static void initializeEntityManager() {
if (em_write == null) {
factory = Persistence.createEntityManagerFactory("parser_writable");
em_write = factory.createEntityManager();
}
明显的问题是为什么会发生这种情况以及如何避免它?
答案 0 :(得分:1)
得到了!
<强> <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
强>
造成了这个麻烦。也许我应该提出错误报告..
答案 1 :(得分:0)
我认为问题是:您在课堂上使用 @SequenceGenerator 尝试仅使用 @SequenceGenerator 和 @GeneratedValue 注释对于 pk / unique 列:
@Id
@SequenceGenerator(name="TTBL_SEQ", sequenceName="timetable", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TTBL_SEQ")
private Long id;