簡體   English   中英

Hibernate 不在數據庫中插入 Springboot 實體

[英]Hibernate not inserting Springboot entities in database

我正在使用 JPA + Spring Boot 將一些數據保存在數據庫中。

我有 2 節課: Plantilla (模板)和Campo (田野)。

Plantilla有一個 ID,即它的 PrimaryKey(PK)。

Campo有一個復合 PK (id_plantilla, campo_name)。 id_plantillaPlantilla的 ForeignKey。 我使用 CampoPK 完成了這項工作, @Embeddable class

我使用@OneToMany @ManyToOne注釋映射了 Plantilla 和 Campo 之間的關系。

所以代碼是這樣的:

植物 Class

@Entity
@Table(name = "plantillas")
public class Plantilla implements Serializable{    
    
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id_plantilla")
private Long id_plantilla;

@Column(length = 50)
private String nombre;

@Column(length = 50)
private String proveedor;
      
@OneToMany(mappedBy = "plantilla",cascade=CascadeType.ALL)
List<Campo> campos = new ArrayList<>();

//getters and setter

坎波 Class

@Entity
@Table(name = "campos")
public class Campo implements Serializable{

@EmbeddedId
private CampoPK campoPK;

@ManyToOne(optional = false)
@JoinColumn(name="id_plantilla", referencedColumnName="id_plantilla",
            insertable=false, updatable=false)
private Plantilla plantilla;

@Column(length = 50)
private double coordX;

@Column(length = 50)
private double coordY;

坎波PK

@Embeddable
public class CampoPK implements Serializable{

@Column(name="id_plantilla",nullable=false)
private Long idPlantilla;    

@Column(name="campo_nombre",nullable=false)
private String campoNombre;

所以,當我啟動應用程序時。 它與數據庫連接並正確創建表。 DB 中的表 Campo

現在,這個應用程序是一個服務器,當它被部署時,它有一個 REST API 你可以調用(我用簡單的表測試了它,並且可以正常插入發送的對象)。 id_plantilla是自動生成的,所以當我發送帶有一些CampoPlantilla時,object Plantilla 沒有 ID。 它有campoNombre ,這是PK的另一個領域

因此,為了測試,我在發送 SQLQuery 時(在接受 HttpRequest 之后)發送了一個帶有 2 個 Campo 對象(材料和尺寸)的 Plantilla,它沒有所需的值,output 是下一個:

為了更清楚,我剪掉了信息消息的第一部分,它們都是這樣的:

2022-05-26 23:09:35.064 DEBUG 13148 --- [nio-8080-exec-4] o.h.e.t.internal.TransactionImpl   :

-首先:看起來ID已經生成,實體被識別,在Plantilla中添加了id,但沒有在Campo實體上,也沒有在Plantilla的List-Campo-上。

Generated identifier: 1, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator
Generated identifier: component[campoNombre,id_plantilla]{id_plantilla=null, campoNombre=material}, using strategy: org.hibernate.id.CompositeNestedGeneratedValueGenerator
Generated identifier: component[campoNombre,id_plantilla]{id_plantilla=null, campoNombre=dimensiones}, using strategy: org.hibernate.id.CompositeNestedGeneratedValueGenerator
committing
Processing flush-time cascades
Dirty checking collections
Collection found: [com.servidorAPELV.springbootServer.entity.Plantilla.campos#1], was: [<unreferenced>] (initialized)    
Flushed: 3 insertions, 0 updates, 0 deletions to 3 objects
Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
Listing entities:
com.servidorAPELV.springbootServer.entity.Campo{coordX=2.0, coordY=2.0, plantilla=null, campoPK=component[campoNombre,id_plantilla]{id_plantilla=null, campoNombre=material}, longX=4.0, longY=1.5}
com.servidorAPELV.springbootServer.entity.Campo{coordX=2.0, coordY=4.0, plantilla=null, campoPK=component[campoNombre,id_plantilla]{id_plantilla=null, campoNombre=dimensiones}, longX=5.0, longY=3.0}
com.servidorAPELV.springbootServer.entity.Plantilla{id_plantilla=1, proveedor=syscam, campos=[com.servidorAPELV.springbootServer.entity.Campo#component[campoNombre,id_plantilla]{id_plantilla=null, campoNombre=material}, com.servidorAPELV.springbootServer.entity.Campo#component[campoNombre,id_plantilla]{id_plantilla=null, campoNombre=dimensiones}], nombre=Prueba plantilla 1}

-其次,我們將 SQL 發送到數據庫:

insert into plantillas (nombre, proveedor, id_plantilla) values (?, ?, ?)
Hibernate: insert into plantillas (nombre, proveedor, id_plantilla) values (?, ?, ?)
insert into campos (coordx, coordy, longx, longy, campo_nombre, id_plantilla) values (?, ?, ?, 
?, ?, ?)
Hibernate: insert into campos (coordx, coordy, longx, longy, campo_nombre, id_plantilla) 
values (?, ?, ?, ?, ?, ?)
o.h.engine.jdbc.spi.SqlExceptionHelper   : could not execute statement [n/a]

我不明白為什么所有值都以“?”出現

- 最后它顯示此消息:

java.sql.SQLIntegrityConstraintViolationException: Column 'id_plantilla' cannot be null

 //A bit forward appears this

2022-05-26 23:09:35.153  WARN 13148 --- [nio-8080-exec-4]o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1048, SQLState: 23000
2022-05-26 23:09:35.153 ERROR 13148 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'id_plantilla' cannot be null
2022-05-26 23:09:35.154  INFO 13148 --- [nio-8080-exec-4] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2022-05-26 23:09:35.158 DEBUG 13148 --- [nio-8080-exec-4] cResourceLocalTransactionCoordinatorImpl : JDBC transaction marked for rollback-only (exception provided for stack trace)

如果有人對正在發生的事情或任何潛在的解決方案或教程有任何了解,我將不勝感激。

PD(因為我無法發布照片,但我將在此處添加 output 的完整圖像): Output 錯誤

好的,您的問題似乎已經有一段時間了,我在這里沒有看到正確的答案,但也許它仍然可以幫助其他人。 我認為您的問題是 MySQL 不支持生成類型 AUTO 或 SEQUENCE。 所以你需要使用 TABLE 或 IDENTITY,第一個更好。

https://thorben-janssen.com/5-things-you-need-to-know-when-using-hibernate-with-mysql/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM