简体   繁体   English

Hibernate 不在数据库中插入 Springboot 实体

[英]Hibernate not inserting Springboot entities in database

I'm using JPA + Spring Boot to persist some data in a Database.我正在使用 JPA + Spring Boot 将一些数据保存在数据库中。

I had 2 classes: Plantilla (Template) and Campo (field).我有 2 节课: Plantilla (模板)和Campo (田野)。

Plantilla has an ID, which is its PrimaryKey(PK). Plantilla有一个 ID,即它的 PrimaryKey(PK)。

Campo has a composite PK (id_plantilla, campo_name). Campo有一个复合 PK (id_plantilla, campo_name)。 id_plantilla is a ForeignKey from Plantilla . id_plantillaPlantilla的 ForeignKey。 I had done this using CampoPK, an @Embeddable class我使用 CampoPK 完成了这项工作, @Embeddable class

I mapped the relation between Plantilla and Campo with the @OneToMany @ManyToOne annotation.我使用@OneToMany @ManyToOne注释映射了 Plantilla 和 Campo 之间的关系。

So the code is this:所以代码是这样的:

Plantilla Class植物 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

Campo Class坎波 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;

CampoPK坎波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;

So, when I launch the app.所以,当我启动应用程序时。 It connects with the DB and create the table correctly.它与数据库连接并正确创建表。 Table Campo in DB DB 中的表 Campo

Now, this App is a Server, when it´s deployed, it has an REST API you can call (I tested it with simple tables and works properly inserting the objects sended).现在,这个应用程序是一个服务器,当它被部署时,它有一个 REST API 你可以调用(我用简单的表测试了它,并且可以正常插入发送的对象)。 The id_plantilla is autogenerated, so when I send a Plantilla with some Campo 's the object Plantilla doesnt have the id. id_plantilla是自动生成的,所以当我发送带有一些CampoPlantilla时,object Plantilla 没有 ID。 It has the campoNombre which is the other field of the PK它有campoNombre ,这是PK的另一个领域

So, for testing, I sent a Plantilla with 2 Campo's Object(material and dimensiones) when the SQLQuery is sent (after the HttpRequest is accepted) it doesn't have the values needed, the output is the next one:因此,为了测试,我在发送 SQLQuery 时(在接受 HttpRequest 之后)发送了一个带有 2 个 Campo 对象(材料和尺寸)的 Plantilla,它没有所需的值,output 是下一个:

To make it more clear, I cut the first part of the info messages, all of them were like this:为了更清楚,我剪掉了信息消息的第一部分,它们都是这样的:

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

-First: It looks that the ID has been generated, the entities are recognized, with the id added in the Plantilla but not on the Campo entities neither on the List-Campo- in the Plantilla. -首先:看起来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}

-Secondly we have the SQL sent to the DB: -其次,我们将 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]

I dont understand why all the values appears with "?"我不明白为什么所有值都以“?”出现

-Finally it shows this message: - 最后它显示此消息:

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)

If someone has any idea of what is happening or any potential solutions or tutorials to check, i will be very grateful.如果有人对正在发生的事情或任何潜在的解决方案或教程有任何了解,我将不胜感激。

PD (as i cant post photos yet i will add the full image of the output here): Output error PD(因为我无法发布照片,但我将在此处添加 output 的完整图像): Output 错误

Ok it seems it has been a while since your question and I don't see a correct answer here but still maybe it helps someone else.好的,您的问题似乎已经有一段时间了,我在这里没有看到正确的答案,但也许它仍然可以帮助其他人。 I think your problem is that MySQL does not support generationtype AUTO or SEQUENCE.我认为您的问题是 MySQL 不支持生成类型 AUTO 或 SEQUENCE。 So you would need to use TABLE or IDENTITY, first one being better.所以你需要使用 TABLE 或 IDENTITY,第一个更好。

https://thorben-janssen.com/5-things-you-need-to-know-when-using-hibernate-with-mysql/ 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