为什么不能在jpa中保存对象

why cant able to save object in jpa

实际上我正在写一个 api 只用于从 table 获取数据所以我不需要创建 table 但我需要记录对 db.i 的每次调用刚刚在 属性 文件中评论了 spring.jpa.hibernate.ddl-auto = update 以避免在启动时创建 table。在评论了 jpa 自动创建代码后,我面临着保存对象的问题(id 为 0 的情况)。

pojos

@Entity
@Table(name = "blabla")

public class ABCD{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private int xxx;
    private Date yyyy;
   //
   //
}

Saving code

model obj = new model(xx, yyy, zzz);
repository.save(obj);

评论该配置条目可能不是一个好主意,根据 documentation 如果没有它,它可以自动设置为 create-drop,我建议您将其更改为 none 以避免任何麻烦。

You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, and create-drop. Spring Boot chooses a default value for you based on whether it thinks your database is embedded. It defaults to create-drop if no schema manager has been detected or none in all other cases. An embedded database is detected by looking at the Connection type. hsqldb, h2, and derby are embedded, and others are not. Be careful when switching from in-memory to a ‘real’ database that you do not make assumptions about the existence of the tables and data in the new platform. You either have to set ddl-auto explicitly or use one of the other mechanisms to initialize the database.