简体   繁体   English

在填充的数据库中使用@GeneratedValue(strategy = GenerationType.IDENTITY)会导致PK约束错误

[英]Using @GeneratedValue(strategy=GenerationType.IDENTITY) in populated database causes PK constraint error

I'm trying to add FeedbackItem items to my database. 我正在尝试将FeedbackItem项目添加到我的数据库中。 The general idea is that an existing object of type Group can receive multiple FeedbackItem objects. 通常的想法是, Group类型的现有对象可以接收多个FeedbackItem对象。 Afterwards I update the object: 之后,我更新对象:

groupDAO.startTransaction();
groupDAO.update(selectedGroup);
groupDAO.commitTransaction();

//in groupDAO
public T update(T entity){
   return em.merge(entity);
}

I'm not suring whether it is JPA causing the issue or not, but the following error will appear: 我不知道是否是JPA引起了问题,但是会出现以下错误:

Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL170430012202680' defined on 'FEEDBACKITEM'. 内部异常:java.sql.SQLIntegrityConstraintViolationException:该语句被中止,因为它会导致在“ FEEDBACKITEM”上定义的“ SQL170430012202680”标识的唯一或主键约束或唯一索引中重复键值。

After some more debugging I found the following entries: 经过更多调试后,我发现以下条目:

[EL Fine]: sql: 2017-04-30 01:22:14.977-- #### VALUES (?, ?) bind => [sampleText, 2] [EL Fine]:sql:2017-04-30 01:22:14.977-- ####值(?,?)bind => [sampleText,2]

[EL Fine]: sql: 2017-04-30 01:22:14.997--ClientSession(889348271)--Thread(Thread[JavaFX Application Thread,5,main])--VALUES(1) [EL Fine]:sql:2017-04-30 01:22:14.997--ClientSession(889348271)-Thread(Thread [JavaFX Application Thread,5,main])-VALUES(1)

Does VALUES(1) mean that it's trying to insert the object under the ID 1? VALUES(1)是否表示它正在尝试将对象插入ID为1? It's the only lead I currently have. 这是我目前唯一的潜在客户。 My database is populated with 10 entries, having an ID ranging from 1 to 10. 我的数据库填充了10个条目,其ID范围为1到10。

Below is the class I'm trying to persist to the database: 下面是我要保留到数据库的类:

@Entity
public class FeedbackItem implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int feedbackId;

    private String feedback;

    @ManyToOne(cascade = CascadeType.PERSIST)
    private User ownerFeedback;

    protected FeedbackItem(){

    }
}

I tested my code without @GeneratedValue(strategy=GenerationType.IDENTITY) and assigned ID's manually. 我测试了没有@GeneratedValue(strategy=GenerationType.IDENTITY)代码,并手动分配了ID。 This works without any issues. 这可以正常工作。

EDIT: below is the Group entity: 编辑:下面是Group实体:

@Entity
public class Group implements IReadOnlyGroup {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int groupId;
    private String name;

    @OneToMany(mappedBy = "group")
    private List<Action> actions;

    @OneToMany(cascade = CascadeType.PERSIST)
    private List<Proposal> proposals;

    @OneToMany(mappedBy = "group")
    private List<Motivation> motivations;

    @OneToOne(mappedBy = "group")
    private GroupState currentState;

    public Groep() {
    }
}

The Group object contains the FeedbackItem objects indirectely. Group对象间接包含FeedbackItem对象。 Each Proposal stores one FeedbackItem 每个Proposal存储一个FeedbackItem

Entity
public class Proposal {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Temporal(TemporalType.TIMESTAMP)
    private Date date = null;

    @ManyToMany
    private List<Action> actions;

    @OneToOne
    private FeedbackItem feedbackitem;

    boolean approved;

    protected Proposal() {

    }

    public Proposal ( List<Action> actions, String feedback, boolean approved, User owner){
        this.date = new Date();
        this.actions = actions;
        this.approved = approved;
        this.feedbackitem = new FeedBackItem(feedback, owner);
    }
}
@OneToMany(cascade = CascadeType.PERSIST) 
private List<Proposal> proposals;

Means that Proposal entities are persisted together with Group, but not merged. 表示投标实体与组保持在一起,但不合并。 Other entities should be persisted separately before assigning and persisting Group entity. 在分配和保留组实体之前,应单独保留其他实体。 I would try remove this cascade attribute, and check if problem still exists. 我将尝试删除此层叠属性,并检查问题是否仍然存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot @GeneratedValue(strategy = GenerationType.IDENTITY) 不起作用 - Spring Boot @GeneratedValue(strategy = GenerationType.IDENTITY) not working 问题 java @GeneratedValue (strategy = GenerationType.IDENTITY) 使用 MariaDB 10.4 和 eclipselink - Problems with java @GeneratedValue (strategy = GenerationType.IDENTITY) using MariaDB 10.4 and eclipselink 注释@Id 和@GeneratedValue(strategy = GenerationType.IDENTITY) 有什么用? 为什么世代类型是身份? - what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity? 为什么在使用策略GenerationType.IDENTITY时Hibernate尝试访问hibernate_sequence? - Why is Hibernate trying to access hibernate_sequence when using strategy GenerationType.IDENTITY? 休眠:@GeneratedValue(strategy = GenerationType - Hibernate: @GeneratedValue(strategy = GenerationType 在为GenerationType.IDENTITY提供标识符值作为生成标识符的策略时,传递给分离的实体 - Detached entity passed to persist when providing identifier value for GenerationType.IDENTITY as strategy to generate the identifier Postgresql 抛出 null 列“id”中的值违反了 GenerationType.IDENTITY 的非空约束 - Postgresql throws null value in column "id" violates not-null constraint for GenerationType.IDENTITY Hibernate GenerationType.IDENTITY 不生成序列 ID - Hibernate GenerationType.IDENTITY not generating sequence ids @ GenerationType.IDENTITY失败10以上 - @GenerationType.IDENTITY failing above 10 全局配置Hibernate以使用GenerationType.IDENTITY - Configure Hibernate globally to use GenerationType.IDENTITY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM