JPA没有为实体生成Id。

huangapple go评论105阅读模式
英文:

JPA not generating Id for entity

问题

我有以下的类:

  1. @Entity
  2. public class Comment {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. @Column(name = "comment_id")
  6. private Long commentId;
  7. @Column(name = "creator_id")
  8. private Long creatorId;
  9. @Column(name = "text")
  10. @ApiModelProperty(value = "Text des Kommentar")
  11. private String text;
  12. @Column(name = "timestamp")
  13. @ApiModelProperty(value = "Zeitstempel der letzten Bearbeitung")
  14. private String timestamp;
  15. protected Comment() {}
  16. public Comment(CommentDto dto) {
  17. this();
  18. updateComment(dto);
  19. }
  20. private void updateComment(CommentDto dto) {
  21. setText(dto.getText());
  22. setCreatorId(dto.getCreatorId());
  23. setTimestamp(UtilService.getTimestampString());
  24. }
  25. }

我从HTTP请求中获取一个包含文本和创建者ID的CommentDto。

据我理解,commentId应该通过调用空构造函数来生成。

在我的Service中,我做了以下操作:

  1. public void addComment(CommentDto comment) {
  2. Comment commentEntity = new Comment(comment);
  3. commentRepository.save(commentEntity);
  4. }

其中commentRepository是一个自动装配的JpaRepository<Comment, Long>

问题是,ID没有生成,我尝试将一个具有空ID的对象插入我的数据库中,从而导致错误。

英文:

I have the following class

  1. @Entity
  2. public class Comment {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. @Column(name = &quot;comment_id&quot;)
  6. private Long commentId;
  7. @Column(name = &quot;creator_id&quot;)
  8. private Long creatorId;
  9. @Column(name = &quot;text&quot;)
  10. @ApiModelProperty(value = &quot;Text des Kommentar&quot;)
  11. private String text;
  12. @Column(name = &quot;timestamp&quot;)
  13. @ApiModelProperty(value = &quot;Zeitstempel der letzten Bearbeitung&quot;)
  14. private String timestamp;
  15. protected Comment() {}
  16. public Comment(CommentDto dto) {
  17. this();
  18. updateComment(dto);
  19. }
  20. private void updateComment(CommentDto dto) {
  21. setText(dto.getText());
  22. setCreatorId(dto.getCreatorId());
  23. setTimestamp(UtilService.getTimestampString());
  24. }

I get a CommentDto from my HTTP-Request consisting of a the text and the creatorId.

As far as I understand, the commentId should be generated through the call of the empty constructor.

In my Service I do the following

  1. public void addComment(CommentDto comment) {
  2. Comment commentEntity = new Comment(comment);
  3. commentRepository.save(commentEntity);
  4. }

With commentRepository being an Autowired JPARepository&lt;Comment, Long&gt;

The problem is, that the ID does not get generated and I get an error for trying to insert an object into my DB with a null id.

答案1

得分: 6

@GeneratedValue(strategy = GenerationType.IDENTITY)
你正在使用 GenerationType.IDENTITY,这意味着使用 IdentityGenerator,它期望数据库中的自增列生成的值,也就是自动递增的。在数据库中将主键设置为自动递增以解决此问题。

或者,你可以使用 GenerationType.AUTO,这是默认的策略。在提交时,AUTO 策略使用全局编号生成器为每个新的实体对象生成主键。这些生成的值在数据库级别是唯一的,永远不会被重新使用。

@Id
@GeneratedValue
@Column(name = "comment_id")
private Long commentId;

英文:
  1. @GeneratedValue(strategy = GenerationType.IDENTITY)

You are using GenerationType.IDENTITY that means IdentityGenerator which expects values generated by an identity column in the database, meaning they are auto-incremented. Make the primary key auto-incremented in database to solve this.

Or you can use GenerationType.AUTO which is the default strategy. During a commit, the AUTO strategy uses the global number generator to generate a primary key for every new entity object. These generated values are unique at the database level and are never recycled.

  1. @Id
  2. @GeneratedValue
  3. @Column(name = &quot;comment_id&quot;)
  4. private Long commentId;

huangapple
  • 本文由 发表于 2020年8月7日 14:46:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63296598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定