0

In my web application I have multiple scheduled services which work on same entities ( like article, customer...etc). If I run a single service at time I've no problem, but when I run two services I get an error because the primary key unique constraint is violated. As primary key I use a generated Long value:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
public Long getId() {
    return id;
}

Every service read data from a txt file, create its entities and every 20 entities execute a flush on hibernate session followed by a clear. Only at the end of the execution of the service the session is committed.

How can I solve?

2
  • I've already a sequence called HIBERNATE_SEQUENCE. I try to change my annotaions but I continue to get the same error. Commented Dec 19, 2014 at 14:40
  • I tried also to print the id assigned to every entity and them are all different, but the error persist! Any idea? Commented Dec 19, 2014 at 18:54

2 Answers 2

3

Oracle supports only sequences for generated keys. Add a sequence to your database:

CREATE SEQUENCE ARTICLE_SEQ;

Change your annotations to:

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="articleSequence")
@SequenceGenerator(name="articleSequence", sequenceName="ARTICLE_SEQ",allocationSize=1) 

It's best to use a separate sequence for each table/type.

Sign up to request clarification or add additional context in comments.

Comments

0

Create sequence in DB and use it like @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_NAME").

1 Comment

I think @SequenceGenerator is also needed, and generator references name of that generator, not sequence name. Sequence name is in @SequenceGenerator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.