I've got the following JPA entity:
#Entity
#Table(schema = "myschema")
#SequenceGenerator(schema = "myschema", name = "seqGenerator",
sequenceName = "person_s1", allocationSize = 1)
public class Person {
#Id
#GeneratedValue(generator = "seqGenerator", strategy = GenerationType.AUTO)
private long id;
the following exceptions are thrown:
Call: DROP SEQUENCE myschema.person_s1
Query: DataModifyQuery(sql="DROP SEQUENCE myschema.person_s1")
[EL Warning]: 2010-11-01 17:21:51.051--ServerSession(10605044)--Exception [EclipseLink- 4002] (Eclipse Persistence Services - 2.1.1.v20100817-r8050):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-02289: sequence does not exist
Error Code: 2289
Call: SELECT myschema.person_s1.NEXTVAL FROM DUAL
Query: ValueReadQuery(sql="SELECT myschema.person_s1.NEXTVAL FROM DUAL")
The sequence is genrated by EclipseLink and the query:
SELECT myschema.person_s1.NEXTVAL FROM DUAL
works fine when used directly...
Any help appreciated
Regards Marcel
the following exceptions are thrown (...)
These traces are generated during schema creation when a particular database object doesn't exist and thus can't be dropped. EclipseLink report such cases as Warning (which are not Error), they can be ignored (you get your sequence, right?).
PS: Why do you use an allocation size of 1, don't you want to benefit from the high/low optimization?
I know this is going to sound really silly, but here it is anyway.
#Entity
#Table(schema = "myschema")
public class Person {
#Id
#SequenceGenerator(schema = "myschema", name = "seqGenerator", sequenceName = "person_s1", allocationSize = 1)
#GeneratedValue(generator = "seqGenerator", strategy = GenerationType.AUTO)
private Long id;
}
Related
I have one entity that already have records in database (20 records).
My primary key column is already defined as SERIAL in a postgres database.
My entity:
#Entity
#Table(name = "foo", schema = "public")
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class FooEntity {
#Id
#SequenceGenerator(name="foo_seq", sequenceName = "foo_seq", initialValue = 20, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "foo_seq")
#Column(name = "id")
private Integer id;
}
I run a query that returns all existing sequences in database and i find the "foo_seq".
In my service, when i try to call my repository to save the data, its returned this error:
FooEntity fooEntity = FooEntity.builder()
.id(null)
.build();
FooEntity savedEntity = fooRepository.save(fooEntity);
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key
value violates unique constraint "foo_pkey" Detail: Key (id)=(5)
already exists.
Seems that spring is not using the value defined in initialValue (should try to save with id 20, but instead, is using your own increment starting with 1). When i call this method again, the increment is added, but not starting with 20.
I'm missing something? Should i have alter this sequence in postgres to start with id 20?
I have the next issue -> I have a table on my db with a composite id...
Supose (Id1,Id2,Id3,Id4), the Id4 is generated by a sequence on the db...
My question is, in spring boot, I generate the entity 'Table1' and the corresponding 'Table1Id',
but when i want to add the corresponding GeneratedValue from the sequence, is not generating anything.
I was looking for in the internet and i found that the GeneratedValue is not working without the #Id anotation, but maybe there are some way to fix this issue.
Thank's and sorry for my english.
SOLVED:
When a composite id is required in your project, it is impossible with an embeddedId. It is necesary to use #IdClass on my compossiteId instead #EmbeddedId, because the second does not work with #GeneratedValues for example my solution was:
#Data
#Entity(name = "table_name")
#IdClass(CompositeIdTest.class)
public class TestClass implements Serializable {
#Id
#Column(name = "column", nullable = false)
private String column;
#Id
#SequenceGenerator(name = "sequence", sequenceName = "sequence", allocationSize = 1)
#GeneratedValue(generator = "sequence")
private int idGenerated;
Anyway, thank's
I'm using spring app with hibernate and postgres to store data. The configuration for the product entity is as follow :
/**
* A Product.
*/
#Entity
#Table(name = "product")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#Document(indexName = "product")
public class Product implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
#SequenceGenerator(name = "sequenceGenerator")
private Long id;
...
}
When I want to create a product using the web app I get the following error duplicate key value violates unique constraint. Detail : the key (id)=(35018) already exists.
From my understanding hibernate use a sequence in db to generate the next id value. So I did SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'; in the psql shell to get all sequence in my db. The output is :
hibernate_sequence
jhi_user_id_seq
key_value_id_seq
jhi_persistent_audit_event_event_id_seq
unit_id_seq
generic_user_id_seq
currency_id_seq
customer_type_id_seq
customer_exploitation_type_id_seq
legal_entity_id_seq
deposit_id_seq
machine_id_seq
tank_id_seq
address_id_seq
product_id_seq
rewarded_file_id_seq
bar_code_type_id_seq
quality_label_id_seq
shop_pdv_id_seq
brand_id_seq
category_id_seq
material_id_seq
ws_call_id_seq
postal_code_id_seq
commune_id_seq
country_id_seq
event_id_seq
event_type_id_seq
key_blob_id_seq
card_id_seq
So I thought nice I have a product_id_seq and I only have to update the value in it for things to work.
But when I request the value with SELECT * FROM product_id_seq; I get :
last_value | log_cnt | is_called
------------+---------+-----------
100616 | 0 | t
So here I think that the id generated for the product id is not coming from this product_id_sequence since it tries to insert a product with id = 35018 and the product_id_seq last value is 100616.
So I wonder where does this id 35018 comes from? Which sequence is used to generate it? My guess is I have to update this mysterious sequence to get things to work. For info the hibernate sequence has a value of 36400.
Any idea that could get me going? Thanks in advance.
You do not map your sequence with postgre sequence so Hibernate creates the sequence hibernate_sequence (the one you got 35018 from) for itself.
To use your existing sequence:
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
#SequenceGenerator(name = "sequenceGenerator", sequenceName = "product_id_seq")
private Long id;
I am configuring a database pointing entity that already exists
#Id
#SequenceGenerator(name = "contacto_generator", sequenceName = "commons.contacto_sequence")
#GeneratedValue(strategy = GenerationType.AUTO, generator = "contacto_generator")
private Long id;
I have the annotations as I was in the old application.
When I upgrade an entity is fine, but when I create a new entity, the sequence does not work well
The sequence "commons.contacto_sequence" is in the 509.
I even activated "show_sql" and run:
select nextval ('commons.contacto_sequence')
If I run it directly in the database, it goes perfectly
Caused by: org.postgresql.util.PSQLException: ERROR: llave duplicada viola restricción de unicidad «pk_contacto»
Detail: Ya existe la llave (id)=(459).
I do not know where the 459 value is, and no sequence in my database has that value
If you look at the console the name of the sequence appears with rare characters
I do not know what it can be or should change, because in theory is something that should automatically do Spring.
If it is because the annotations are deprecated and does not connect well to the sequence or that can be.
This project has Jpa 2.1, Spring 4.3.x
Try use GenerationType.SEQUENCE strategy:
#Id
#SequenceGenerator(name = "contacto_generator", sequenceName = "commons.contacto_sequence")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "contacto_generator")
private Long id;
I have an entity with a persistent field declared as Serializable.
I would like to build a query with the CriteriaBuilder, that filters the results by the Serializable field.
The database is Oracle, and the field type is RAW(255) as hbm2ddl defined it.
If i write the query with a plain JPQL TypedQuery, everything works fine (the Serializable field is the one with the name "entityId"):
TypedQuery<Change> query = em.createQuery("FROM Change c WHERE c.entityClass = :class AND c.entityId = :id", Change.class);
query.setParameter("class", Person.class.getName());
query.setParameter("id", new Integer(2287));
query.getResultList();
However, the very same query with criteria builder does not work:
final CriteriaBuilder builder = em.getCriteriaBuilder();
final CriteriaQuery<Change> criteriaQuery = builder.createQuery(Change.class);
final Root<Change> from = criteriaQuery.from(Change.class);
final CriteriaQuery<Change> select = criteriaQuery.select(from);
final List<Predicate> predicates = new ArrayList<>();
predicates.add(builder.equal(from.get("entityClass"), Person.class.getName()));
predicates.add(builder.equal(from.get("entityId"), new Integer(2287)));
select.where(predicates.toArray(new Predicate[predicates.size()]));
final TypedQuery<Change> query = em.createQuery(select);
query.getResultList();
It throws the following exception after invoking getResultList():
[2013-05-21 16:12:45,960] [com.mycompany.myproduct.server.Main.startServer(Main.java:56)] [ERROR] [main] - Error starting Server: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
...
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
...
Caused by: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected BINARY got NUMBER
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)
...
Change.java:
#Entity
#Table(name = "T_REVISION_CHANGE")
#SequenceGenerator(name = "seq_revision_change", sequenceName = "SEQ_REVISION_CHANGE", allocationSize = 1)
public class Change {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_revision_change")
private Integer id;
#Column(name = "ENTITY_CLASS")
private String entityClass;
#Column(name = "ENTITY_ID")
private Serializable entityId;
}
I tried to manually serialize the Integer but the same kind of exception was thrown saying that a Serializable instance was expected instead of a byte array... :)
Any comment would be much appreciated.