简体   繁体   中英

Hibernate not inserting Springboot entities in database

I'm using JPA + Spring Boot to persist some data in a Database.

I had 2 classes: Plantilla (Template) and Campo (field).

Plantilla has an ID, which is its PrimaryKey(PK).

Campo has a composite PK (id_plantilla, campo_name). id_plantilla is a ForeignKey from Plantilla . I had done this using CampoPK, an @Embeddable class

I mapped the relation between Plantilla and Campo with the @OneToMany @ManyToOne annotation.

So the code is this:

Plantilla 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

@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

@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

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). The id_plantilla is autogenerated, so when I send a Plantilla with some Campo 's the object Plantilla doesnt have the id. It has the campoNombre which is the other field of the 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:

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.

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:

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

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. So you would need to use TABLE or IDENTITY, first one being better.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM