java c auto,Java JPA GenerationType.Auto值始终为空

I have several classes in the same project in which the id is always autogenerated. However in this specific case the value of my id happens to be always null. I'm really wondering what is wrong, because after some changes it stopped working. And comparing it with the other classes the code seems to be the same.

package com.checkin.model.entity;

import javax.persistence.*;

@Entity

public class Checkin {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private Long id;

@Column(nullable = false)

private String date;

public Checkin(){

}

public Checkin(String date){

this.date = date;

System.out.println(this.id);

System.out.println(this.getId());

}

public Long getId() {return id;}

public void setId(Long id) {this.id = id;}

public String getDate() {return date;}

public void setDate(String date) {this.date = date;}

}

This is the repository where the objects are saved:

package com.checkin.model.repository;

import com.checkin.model.entity.Checkin;

import org.springframework.data.repository.CrudRepository;

import org.springframework.stereotype.Repository;

@Repository("checkinRepository")

public interface CheckinRepository extends CrudRepository {

Checkin findById(Long id);

}

and finally this is the result that I get from Postman

解决方案

In this case, what you want to check is if the H2 database id column has the attribute to generate IDs. I am guessing it does not. I dont use AUTO much myself so I am assuming if your Auto works for other Entities then those are setup correctly.

You may need to do one of the following:

migration here

update column in H2

recreate the table

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值