JPA没有为实体生成Id。

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

JPA not generating Id for entity

问题

我有以下的类:

@Entity
public class Comment {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "comment_id")
  private Long commentId;

  @Column(name = "creator_id")
  private Long creatorId;

  @Column(name = "text")
  @ApiModelProperty(value = "Text des Kommentar")
  private String text;

  @Column(name = "timestamp")
  @ApiModelProperty(value = "Zeitstempel der letzten Bearbeitung")
  private String timestamp;

  protected Comment() {}

  public Comment(CommentDto dto) {
    this();
    updateComment(dto);
  }

  private void updateComment(CommentDto dto) {
    setText(dto.getText());
    setCreatorId(dto.getCreatorId());
    setTimestamp(UtilService.getTimestampString());
  }
}

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

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

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

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

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

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

英文:

I have the following class

  @Entity
  public class Comment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;comment_id&quot;)
    private Long commentId;

    @Column(name = &quot;creator_id&quot;)
    private Long creatorId;

    @Column(name = &quot;text&quot;)
    @ApiModelProperty(value = &quot;Text des Kommentar&quot;)
    private String text;

    @Column(name = &quot;timestamp&quot;)
    @ApiModelProperty(value = &quot;Zeitstempel der letzten Bearbeitung&quot;)
    private String timestamp;


    protected Comment() {}
    
    public Comment(CommentDto dto) {
      this();
      updateComment(dto);
    }

    private void updateComment(CommentDto dto) {
      setText(dto.getText());
      setCreatorId(dto.getCreatorId());
      setTimestamp(UtilService.getTimestampString());
    }

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

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

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;

英文:
@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.

@Id
@GeneratedValue
@Column(name = &quot;comment_id&quot;)
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:

确定