0

I get the following error:

javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.xx.xx.xx.S3ObjectsEntity

I already tried this without any effect:

None of the above will work and I get the same error message.

This is my Entity.java:

@Entity
@Table(name = "s3objects", schema = "public", catalog = "CustomerReportBDD")
public class S3ObjectsEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "s3objects_id",unique=true, nullable = false)
private Long s3Objects_Id;
@Basic
@Column(name = "etag", nullable = true, length = 100)
private String etag;
@Basic
@Column(name = "bucketname", nullable = true, length = 100)
private String bucketname;
@Basic
@Column(name = "objectkey", nullable = true, length = 100)
private String objectkey;
@Basic
@Column(name = "lastmodified", nullable = true, length = 100)
private String lastmodified;
@Basic
@Column(name = "storageclass", nullable = true, length = 100)
private String storageclass;
@Basic
@Column(name = "owner_id", nullable = true)
private Integer owner_id;

And the sql script that I used to create the table :

DROP TABLE IF EXISTS s3objects CASCADE;
CREATE TABLE s3objects (
s3objects_id   SERIAL,
etag          varchar(100),
bucketname    varchar(100),
objectkey     varchar(100),
lastmodified  varchar(100),
storageclass  varchar(100),
owner_id      varchar(100) ,
CONSTRAINT s3objectsKey PRIMARY KEY(s3objects_id)
);

The owner id is nullable in the base.

I works perfectly, when I manually add all fields in the base (with s3object_id & owner_id at null)

I tried to do that on the entity:

@GeneratedValue ( strategy=GenerationType.AUTO)

And that:

@SequenceGenerator(name="s3ObjectSequence", sequenceName = "s3objects_s3objects_id_seq",allocationSize = 1)
@GeneratedValue ( strategy=GenerationType.SEQUENCE, generator = "s3ObjectSequence")

s3objects_s3objects_id_seq is a sequence present in the base after the creation of the table in the database.

Edit 1:

added the option

<properties>
  <property name="hibernate.jdbc.fetch_size" value="1000" />
  <property name="show_sql" value="true"/>
  <property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>

In my persist.xml

The tables are created from entities but I always get the same error message.

Slater
  • 817
  • 2
  • 10
  • 16

1 Answers1

0

Each database driver implements Id generation strategy in a different way. Which driver are you using? JPA/hibernate version?

Try to put this hibernate option "hibernate.hbm2ddl.auto=create" in a development environment. It will create your table and the sequence automatically. Copy the generated schema in your script.

If you put @Id, you do not need to write "unique=true, nullable = false" on the column.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "s3objects_id")
private Long s3Objects_Id;
Beñat Bermejo
  • 361
  • 1
  • 9
  • where can I see that ? ( what file ) ? And thanks for the precision on @id – Slater Apr 05 '16 at 08:19
  • Are you using a persistence.xml file? Spring boot? Spring? Where is defined the persistence provider, datasource, entity manager..? – Beñat Bermejo Apr 05 '16 at 08:22
  • Haaa i ot a datasource.xml : And i'm using Spring ( i'm beginner on that fwk ) I got a : -persistenceunit.xml -persist.xml In the project – Slater Apr 05 '16 at 08:23
  • Ok finally found what you meant in your message. I'm editing the question to add your hibernate option ( not working ) – Slater Apr 05 '16 at 12:31
  • @Slater Which postgres driver library are you using? Hibernate version? Do u have ManyToOne mappings? – Beñat Bermejo Apr 05 '16 at 12:48
  • No many to one mapping. Only this Entity actually no link done on this table ( but it will in near future ) For the postgresql : I guess you mean that And for hibernates : hibernate version 5.0.6.Final – Slater Apr 05 '16 at 13:03
  • Driver jdbc : 9.4.1201 ( jdbc4) – Slater Apr 05 '16 at 13:12
  • The code looks correct and should work. @GeneratedValue(strategy = GenerationType.IDENTITY) should do the work... If you want to use hibernate and Spring and you are starting a new project, I recommend you to use Spring Boot with Spring Data JPA. You have an example project https://spring.io/guides/gs/accessing-data-jpa/ – Beñat Bermejo Apr 05 '16 at 13:24