繁体   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