Getting wrong values from sequence
In weblogic 11, I have 2 manages severs under 1 cluster. I have created 1 datasource on Admin server and attached to cluster. In hibernate side I am using
#XmlTransient
#Id
#Column(name = "id", nullable = false)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_NAME")
#SequenceGenerator(name = "SEQ_NAME", sequenceName = "SEQ_NAME")
public Integer getId() {
return this.id;
}
Exception:
org.hibernate.event.def.AbstractSaveEventListener saveWithGeneratedId - generated identifier: 41813, using strategy: org.hibernate.id.SequenceHiLoGenerator
org.hibernate.util.JDBCExceptionReporter logExceptions - Could not execute JDBC batch update ......bla..bla..java.sql.BatchUpdateException: ORA-00001: unique constraint
Assumption is only on 2nd managed server I am facing this problem.
Can anyone suggest me please.
This might have nothing to do with the server configuration(Cluster etc).
You need to use the allocationSize option.
#SequenceGenerator(name = "productprice_productpriceid_seq", sequenceName = "productprice_productpriceid_seq", allocationSize=1)
Refer below for more details.
https://forum.hibernate.org/viewtopic.php?f=1&t=992448
Related
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
#SequenceGenerator(name = "generator", schema = "MD", sequenceName = "sq_base_class")
public Long getId() {
return id;
}
Hi! I have entity which using MS SQL Sequence for generating ID, but value is incorrect.
com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_BCL'. Cannot insert duplicate key in object 'MD.BASE_CLASS'. The duplicate key value is (551009).
Example: SequenceGenerator set ID = 551009, but select next value for md.sq_base_class return 551115. How to resolve it?
Hibernate-version: 5.3.10.Final
Maybe you lost the allocation size = 1 in #SequenceGenerator
I am trying to set up table entities with primary key id's generated from sequences I previously defined and used in an Oracle DB.
I am trying to use the Sequence Generator and Generated Value annotations but they don't seem to work quite right for me. I'm unsure what I am missing/doing wrong.
#Id
#Column(name = "ID")
#GeneratedValue(Strategy=GenerationType.SEQUENCE, generator = "seq")
#SequenceGenerator(name="seq", sequenceName = "id_seq", allocationSize = 1)
private long id;
This does not seem to work. Any advice on how to solve this problem, or a usable workaround, would be appreciated. This uses an Oracle 11 DB and JPA 2.
This is an example, working with JPA 2.0, of how to get an id from an Oracle sequence.
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MY_JPA_ID_GENERATOR")
#SequenceGenerator(name = "MY_JPA_ID_GENERATOR", allocationSize = 1, sequenceName = "MY_ORACLE_SEQUENCE_NAME")
#Column(name = "ID", unique = true, nullable = false, updatable = false)
private Long ID;
i have 2000 record in my DB . after inserting a new record the sequnce start from 1 instead of 2001. In my hibernate POJO i have this following
#Id
#SequenceGenerator(name = "SEQ_USER_MASTER_ID", sequenceName = "SEQ_USER_MASTER_ID", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_USER_MASTER_ID")
private Long id;
what i need to do in hibernate java to automatically start from 2001 or what ever the max id in table . the sequence should start greater than it . i am struck on this .
ORA-02289: sequence does not exist, error in hibernbate
In Oracle you can't autogenerate values, you should create a sequence (let's call it VEHICLE_SEQ). Then you should put this annotations on your id:
#GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ")
#SequenceGenerator(name = "SEQ", sequenceName = "VEHICLE_SEQ")
To create the sequence:
CREATE SEQUENCE VEHICLE_SEQ START WITH 1 INCREMENT BY 1;
You can use as below, if you are not really focussing only on sequence number to generate the ID
#Column(name = "XYZ", nullable = false)
#GeneratedValue(generator = "uuid.hex")
#GenericGenerator(name = "uuid.hex", strategy = "uuid.hex")
#Id
This generates the new number whenever a new records gets inserted
I need a simple sequence which would give me incremented integers that I later on use as part of a String.
I made this sequence using postgresql command line:
CREATE SEQUENCE my_seq
INCREMENT BY 1
The sequence exists as I can query it from postgresql command line, But I'm trying to get the values using hibernate:
Query query = session.createSQLQuery("select nextval(:sequence);");
query.setParameter("sequence", "my_seq");
Long nextVal=((BigInteger)query.uniqueResult()).longValue();
And I am getting this exception:
ERROR: relation "my_seq" does not exist
The sequence values do NOT represent the attribute of any entity. I only need them to store the number of logins, but I do not store the logins as entities.
EDIT: I got it working by adding the scheme name to the query:
String query1 = "select nextval(myscheme.my_seq)";
Query query = session.createSQLQuery(query1);
I can't figure out why it needed the scheme though, as myscheme was already default and all other queries worked fine without specifying the scheme. If anyone can shed some light I shall accept its answer.
You don't need to map a sequence to an entity.
Just leave the mapping as follow for the ID column of your entity, and the id of the tntity will be generated from the sequence my_seq. You don't need to call nextval(my_seq) or anything else.
#Entity("myEntity")
public class MyEntity {
#Id
#Column(name = "id", unique = true, nullable = false, insertable=false)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
#SequenceGenerator(name = "my_seq", sequenceName = "my_seq", allocationSize = 1)
private long id;
}
Below is the logic which will use a sequence called "my_seq" from the database and map it to the column "column_name" table "table_name"
Soon after you import the classes the sequence generator should be placed before you begin the class
#Entity
#org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
#Table(name = "table_name")
#SequenceGenerator(name = "my_seq", sequenceName = "my_seq")
public class ClassName implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "my_seq")
#Column(name = "column_name")
private Long columnName;
}
Let me know if this is useful.
In order to avoid hardcoding schema to your query, you can set hibernate.default_schema property in your persistence.xml as follows.
<property name="hibernate.default_schema" value="myscheme"/>
This could be also used in spring configuration file as is stated here.