My pojo definition worked fine till I used Postgres 9.x, When I changed the database to postgres 12 I get Sequence not found error when inserting a new row to a table. When I debugged the sql statements run in the database I found that hibernate ignored the schema name.
Postgres 9 : select nextval('ita.ita_settings_is_is_id_seq')....
Postgres 12: select nextval('ita_settings_is_is_id_seq')....
My Pojo definition is as follows
#Entity
#Table(name = "ita.ita_settings_is")
#SequenceGenerator(name = "settingItaIdSeq", sequenceName = "ita.ita_settings_is_is_id_seq", initialValue = 1, allocationSize = 1)
public class itaSettings implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY, generator = "settingitaIdSeq")
#Column(name = "is_id")
private int id;
Related
In spring boot JPA I tried to implement sequence generator but it is not working.
the following is my entity
#Entity
#Table(name = "role_level")
public class RoleLevel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "role_level_sequence", sequenceName = "role_level_id_seq",allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "role_level_sequence")
#Column(name = "id", updatable = false)
private Long id;
#Column(name = "role_level")
private String roleLevel;
#Column(name = "role_level_description")
private String roleLevelDescription;
//getters and setters
}
when I insert value in directly through the database then next sequence from the db is not getting in jpa.it shows
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "role_level_pkey"
Detail: Key (id)=(7) already exists.
But the console shows
Hibernate: select nextval ('role_level_id_seq')
I think its not working.
Is there any solution for this.?
I'm using Hibernate 4.3.11.
I have the following entity:
#Entity
#Table(name="ESP_RETARD")
public class ESP_RETARD implements Serializable
{
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "pk_seqret", sequenceName = "ESP_RETS_SEQ1", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_seqret")
#Column(nullable = false)
#NotNull
private long idRetard;
I tried to persist a new entity on ESP_RETARD.
But, I encounter a problem: a new entity is added, however, it's with id 0.
the second time to add a new one, I got this exception:
javax.persistence.PersistenceException:
org.hibernate.exception.ConstraintViolationException: could not
execute statement
...
Caused by: org.hibernate.exception.ConstraintViolationException: could
not execute statement
...
Caused by: java.sql.SQLException: ORA-00001: unique constraint
(EDTINGA.SYS_C009525) violated
Knowing that: when I used:
Hibernate 3 with
those jars,
I didn't get this issue.
But once I upgraded to Hibernate 4.3.11 with those jars, I encountred this excpetion.
==> So it's a problem of jars, I suppose that it's missed jar(s) have to be added to Hibernate 4.3.11.
Have you please any idea about solving that issue ?.
Big thanks Sir.
You can try removing the entire entity and then re-create it with new table name and new column name for the id.
Don't forgot to change the nomination on #SequenceGenerator and #GeneratedValue.
Indeed, it's not necessary to use neither #Column(nullable = false) nor #NotNull.
#Id
#SequenceGenerator(name = "pk_seqret1", sequenceName = "RETS_SEQ1", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_seqret1")
private long idRetard;
HTH.
In spring data jpa Application I created one model with #entity annotation. I am saving that model data into table. I am auto incrementing primary key. But when I am saving data into table it's not sequentially auto incrementing.
#GeneratedValue(strategy = GenerationType.AUTO)
class file
#Entity
#Table(name="exception")
public class Exception implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "exception_seq_generator")
#SequenceGenerator(name = "exception_seq_generator", sequenceName = "exception_seq")
#Column(name="exception_id")
public Integer exceptionId;
#Column(name="status_code")
public Integer statusCode;
public String message;
public String status;
public String error;
//Getter and setter
Table
can any one tell me why primary key is not auto incrementing sequentially? why it's not taking 2,3,4.....
First of all try setting the allocationSize:
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "exception_seq_generator")
#SequenceGenerator(name = "exception_seq_generator", sequenceName = "exception_seq", allocationSize=1)
#Column(name="exception_id")
public Integer exceptionId;
Also check your current Sequence in the Database, it might have a wrong value now.
Simpler aproach:
Define the primary-key column in PostgreSQL DB as SERIAL:
CREATE TABLE xy (
id SERIAL PRIMARY KEY;
);
And annotate the Column with:
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
The SERIAL type, creates an auto-increment sequence for you and you don't have that much overhead in you JPA Entities.
I have a MyClass entity with an auto increment id at the time of the project deployment I have an init.sql file to initialize the MY_CLASS table by adding two lines
the problem when I use my web service REST to insert a new line in the MY_CLASS table I have an error message of duplicate key of id 1 for the first click and id 2 on the second click but after the POST goes without problems. To solve this problem I can add the following line in my init.sql file
ALTER SEQUENCE MY_CLASS_id_seq RESTART WITH 3;
My question: How can I configure my POST to persist the data with the last id because whenever I can insert data with SQL.
#Entity
#Cacheable
#Getter
#Setter
#Table(name = "MY_CLASS")
public class MyClass {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
private String label;
}
#RepositoryRestResource(collectionResourceRel = "clazz", path = "clazz")
public interface MyClassRepository extends PagingAndSortingRepository<MyClass, Long> {
}
init.sql
INSERT INTO public.MY_CLASS (label) values('label_1');
INSERT INTO public.MY_CLASS (label) values('label_2');
Wouldn't you use the sequence generator, would you?
public class MyClass {
public static final String SEQUENCE_NAME = "MY_CLASS_id_seq";
#Id
#GeneratedValue(strategy = SEQUENCE, generator = SEQUENCE_NAME)
#SequenceGenerator(name = SEQUENCE_NAME, sequenceName = SEQUENCE_NAME)
private Long id;
#NotNull
private String label;
}
I have these two entities:
Anagrafica
#Entity
#Access(AccessType.FIELD)
#Table(name = "S_MC_CC_USER")
#SequenceGenerator(name = "SEQ_ID", sequenceName = "SEQ_ID", allocationSize = 1)
public class Anagrafica implements Serializable{
private static final long serialVersionUID = 332466838544720886L;
#EmbeddedId
private AnagraficaId anagraficaId;
#Column(name = "USER_ID")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ID")
private Long userId;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "USER_ID", updatable = false, insertable = false)
private List<Mobile> mobiles;
/**
* La classe di dominio che modella la chiave primaria di un {#link Anagrafica}
*
* #author Massimo Ugues
*
*/
#Embeddable
static public class AnagraficaId implements Serializable {
private static final long serialVersionUID = -54640203292300521L;
#Column(name = "ANAG_UTENTE")
private String bt;
#Column(name = "COD_ABI")
private String abi;
public AnagraficaId() {
super();
}
Mobile
#Entity
#Table(name = "S_MOBILE")
#SequenceGenerator(name = "SEQ_MOBILE", sequenceName = "SEQ_MOBILE", allocationSize = 1)
public class Mobile implements Serializable{
private static final long serialVersionUID = 5999493664911497370L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_MOBILE_DEVICE_REGISTRY")
#Column(name = "ID_MOBILE")
private Long mobileId;
#Column(name = "DEVICE_TOKEN")
private String deviceToken;
#Column(name = "DATA_INSERIMENTO")
#Temporal(TemporalType.TIMESTAMP)
private Calendar dataInserimento = Calendar.getInstance();
With eclispe-link 2.1.2 all works great, but with eclispe-link 2.5.1 I got this exception:
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [persistence-unit] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The #JoinColumns on the annotated element [field mobiles] from the entity class [class com.intesasanpaolo.domain.entities.sub.Anagrafica] is incomplete. When the source entity class uses a composite primary key, a #JoinColumn must be specified for each join column using the #JoinColumns. Both the name and the referencedColumnName elements must be specified in each such #JoinColumn.
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1954)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1945)
at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:322)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:288)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
... 40 more
The problem is the OneToMany association based on a foreign key that is not primary key.
Since I cannot change the database model how can I make it work?
Kind regards
Massimo
The reason it worked in a prior version was that it EclipseLink doesn't look at the fields in the mapping, but with JPA adding derived Id support, EclipseLink now validates the number of foreign keys match the number of ID fields.
James' answer here
JPA #JoinColumn issues while joining on non primary key columns
explains it that you'll need to use a descriptorCustomizer to change the JPA mapping. So you would either not map the field in JPA (mark it as #Transient) and then add a mapping in the customizer, or have the JPA mapping to use all primary key fields and then change the mapping in the customizer to only use the USER_ID->USER_ID fields.
EclipseLink customizers are shown here:
http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_customizer.htm
Ok, this is the Customizer I created:
public void customize(ClassDescriptor descriptor) throws Exception {
// handle the oneToManyMapping to non foreign keys
ManyToManyMapping mapping = (ManyToManyMapping) descriptor.getMappingForAttributeName("mobileDevices");
ExpressionBuilder builder = new ExpressionBuilder();
mapping.setSelectionCriteria(builder.getField("USER_ID").equal(builder.getParameter("USER_ID")));
// handle the insert statement
mapping.setInsertCall(new SQLCall(""));
}
As suggested from Chris this works great with the selection.
I had to modify the Insert Call since eclipse-link tried to create and insert statement on a mapping table that I haven't.
The problem now is on the delete: when I try to delete the collection from the source association (i.e. Cliente) as described here
Cliente.ClienteId id = new Cliente.ClienteId(abi, bt);
Cliente cliente = clienteRepository.findOne(id);
cliente.setMobileDevices(null);
I need eclipse link to delete the orphan.
The dml generated is the following:
DELETE FROM S_MC_CC_CLIENTI_S_MOBILE_DEVICE_REGISTRY WHERE ((mobileDevices_ID_MOBILE_DEVICE_REGISTRY = 13) AND ((ANAG_UTENTE = '71576493') AND (COD_ABI = '01025')))
Since I haven't the mapping table I modified the customizer adding a setDeleteCall statement :
mapping.setDeleteCall(new SQLCall("DELETE FROM S_MOBILE_DEVICE_REGISTRY WHERE USER_ID = #USER_ID"));
In this way eclipse link generates 2 dml:
DELETE FROM S_MOBILE_DEVICE_REGISTRY WHERE USER_ID = NULL
DELETE FROM S_MOBILE_DEVICE_REGISTRY WHERE (ID_MOBILE_DEVICE_REGISTRY = 13)
The first is the translation of my SQLCall, but without the correct parameter: any idea how to generate only the correct delete statement?
Kind regards.
Massimo