英文:
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 = "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());
}
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<Comment, Long>
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 = "comment_id")
private Long commentId;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论