hello :-)
DB2 offers the Sequence-Generator and I added it successfullly in a DB2-Database called "ProductStore":
Code:
CREATE SEQUENCE mySequence
AS BIGINT
START WITH 10
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
NO CYCLE
NO CACHE
NO ORDER
In my Java-Entity "Product" I added the @SequenceGenerator successfully:
Code:
@Entity
@SequenceGenerator(name="mySequence_PRODUCT", sequenceName="mySequence", initialValue=10, allocationSize=1)
@Table(name = "PRODUCT")
public class Product implements Serializable{
...
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="mySequence_PRODUCT")
@Column(name = "ID_PRODUCT", nullable = false, insertable = true, updatable = true, length = 10)
public int getIdProduct() {return idProduct;}
...
}
I have 2 ways to store Product-Instances in the database:The first way:I have a web-frontend in which Users can save (!) an instance of an PRODUCT-Entity. When I open the DB2-Database I see tha ID_PRODUCT is successfully incremented.
The second way:I have also an Excel-Sheet, in which other Users can also save Products in the database and which is
absolutely needed to be isolated from the web-client (jboss, hibernate, etc).
So far, so good..all works with a little problem:
One user save two records via the "second way" (excel-sheet).
The database increment it:
Code:
ID_PRODUCT: 1
ID_PRODUCT: 2
The other user save one record via the "first way" (web-frontend):
When the Web-User push the "Save the record"-Button, HTTP-STATUS 404 appears and the JBOSS-Console says:
Code:
"Could not execute JDBC batch update".
(Because "ID_PRODUCT 1" already exists).
When the Web-User goes back and will try to save it again, it comes again to
Code:
"Could not execute JDBC batch update".
(Because "ID_PRODUCT 2" (!) already exists).
But when the User now goes back and will try to save it again,
works! Because the database saves it now with the unique "ID_PRODUCT: 3"
My Question:
Is there a way to store it correctly without the need to "push the back-button". (I do not want to use the IDENTITY-Function).
I have a solution but this is not so good:
I take the method:
Code:
public void setIdProduct(int idProduct) {
this.idProduct = idProduct;
...
}
and call within this method the sql-query to get the last (max) ID-Value:
Code:
select max(idProduct) from Product
and set the idProduct-instance manually with max(idProduct)+1.
But this is not so performant and with that solution I can not use the advantageous of the Sequence-Generator.
Another way ist to set the "initialValue" and the "allocationSize" big enough to avoid the conflict with the excel-sheet. But this is not good solution.
Code:
@SequenceGenerator(name="mySequence_PRODUCT", sequenceName="mySequence", initialValue=100, allocationSize=1)
Do u have a better way to manage that????
Thanks for every tip![/code]