【发布时间】:2017-05-28 18:44:59
【问题描述】:
我在同一个项目中有几个类,其中的 id 总是自动生成的。但是,在这种特定情况下,我的 id 值恰好始终为空。我真的想知道出了什么问题,因为经过一些更改后它停止了工作。并且将它与其他类进行比较,代码似乎是相同的。
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;}
}
这是保存对象的存储库:
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, Long> {
Checkin findById(Long id);
}
最后这是我从 Postman 那里得到的结果
【问题讨论】:
-
底层数据库是什么?并显示 id 列的 DDL 定义
-
我不确定你所说的 DDL 定义是什么意思。我将 Hibernate 与 JPA 一起使用。关于您关于底层数据库的问题,这是我在 build.gradle 中使用的内容: compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("com.h2database:h2" )
-
@AntonioDelaTorre 设置休眠以重新创建数据库表(和丢失的信息)并启用 SQL 日志。您将在日志中有
DDL(SQLCREATE TABLE语句)。其他生成 DDL 的方式:geowarin.github.io/generate-ddl-with-hibernate.html。它有点复杂。
标签: java json hibernate jpa persistence