I am performing hibernate jpa batch update and its giving me following error
2015-04-21 15:53:51,907 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (Thread-283 (HornetQ-client-global-threads-462057890)) SQL Error: 0, SQLState: 42P01
2015-04-21 15:53:51,908 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (Thread-283 (HornetQ-client-global-threads-462057890)) ERROR: relation "my_seq_gen" does not exist
I am using postgres database and my ID is auto generated
@Id
@SequenceGenerator(name="seq-gen",sequenceName="MY_SEQ_GEN"initialValue=205, allocationSize=12)
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="seq-gen")
@Column(name="\"ID\"",unique=true,nullable=false)
private int id;
This is my batch insert code snippet
getEm().getTransaction().begin();
System.out.println("transaction started--------------");
try {
for (Receipt ReceiptEntity : arrReceiptEntity) {
getEm().persist(ReceiptEntity);
}
getEm().getTransaction().commit();
System.out.println("commited");
} catch (Exception exception) {
System.out.println("error----------------------------------------------------------------------");
if(getEm().getTransaction().isActive())
getEm().getTransaction().rollback();
LOG.error(exception);
} finally {
getEm().flush();
getEm().clear();
getEm().close();
}
I have added the following property in persistence.xml
<property name="hibernate.id.new_generator_mappings" value="true"/>
Please suggest what i am doing wrong.
If you don't want to change your entity definition, then you need to create a sequence in your postgreSQL schema with name hibernate_sequence
.
CREATE SEQUENCE hibernate_sequence START 1;
UPDATE:
You are missing second sequence generatef, which you defined for your entity, just add it like previous one:
CREATE SEQUENCE my_seq_gen START 1;
What is a sequence?
Sequence is an ordered list of integers. The orders of numbers in the sequence are important. You can configure what is the min and max values, by what amount you should increment it:
CREATE SEQUENCE [ IF NOT EXISTS ] sequence_name
[ AS { SMALLINT | INT | BIGINT } ]
[ INCREMENT [ BY ] increment ]
[ MINVALUE minvalue | NO MINVALUE ]
[ MAXVALUE maxvalue | NO MAXVALUE ]
[ START [ WITH ] start ]
[ CACHE cache ]
[ [ NO ] CYCLE ]
No you can use functions like nextval('')
in your SQL commands and in hibernate to get next value from the set. This is much cheaper than keepipng current primary key value in a sequence_table or looking for max PK value in existing table. So it provides an easy and cheap way to find next PK for given table.
All tables usually use a dedicated Sequance, and like in this example it was chosen as IdGenerator strategy.
Sore useful tutorial:
Try to annotate your id
with @Id
and @GeneratedValue(strategy = GenerationType.IDENTITY)
.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
UPDATE: It will only work if your id column was declared as SERIAL
or BIGSERIAL
types.
I hope you get the answer but if you are still finding the answer this could be helpful.
I had same problem and resolved it annotating getter method of the id with @SequenceGenerator
and @GeneratedValue
.
@SequenceGenerator(name="seq-gen",sequenceName="MY_SEQ_GEN", initialValue=205, allocationSize=12)
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="seq-gen")
public int getId(){
return id;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With