我收到以下错误:
javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.xx.xx.xx.S3ObjectsEntity
我已经尝试过这个没有任何效果:
ids for this class must be manually assigned before calling save():
Hibernate: ids for this class must be manually assigned before calling save()
ids for this class must be manually assigned before calling save()
上述所有内容都不起作用,我收到相同的错误消息。
这是我的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;
我用来创建表的sql脚本:
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)
);
所有者ID在基础中可以为空。
我工作完美,当我手动添加基础中的所有字段时(s3object_id& owner_id为null)
我试图在实体上做到这一点:
@GeneratedValue ( strategy=GenerationType.AUTO)
那:
@SequenceGenerator(name="s3ObjectSequence", sequenceName = "s3objects_s3objects_id_seq",allocationSize = 1)
@GeneratedValue ( strategy=GenerationType.SEQUENCE, generator = "s3ObjectSequence")
s3objects_s3objects_id_seq是在数据库中创建表后基础中存在的序列。
编辑1:
添加了选项
<properties>
<property name="hibernate.jdbc.fetch_size" value="1000" />
<property name="show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
在我的persist.xml
表是从实体创建的,但我总是得到相同的错误消息。
答案 0 :(得分:0)
每个数据库驱动程序以不同的方式实现Id生成策略。你在用哪个司机? JPA / hibernate版本?
尝试在开发环境中放置此hibernate选项“hibernate.hbm2ddl.auto = create”。它将自动创建您的表和序列。在脚本中复制生成的模式。
如果你输入@Id,你不需要在列上写“unique = true,nullable = false”。
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "s3objects_id")
private Long s3Objects_Id;