У меня нет исключений, и все вроде бы нормально, но после вставки некоторых данных в базу они не сохраняются. Я не знаю, нужна ли мне какая-то конкретная аннотация c, или я потерпел неудачу в какой-то конфигурации Spring.
Бин сущности:
@Entity
@Table(name="IMAGEN")
public class Imagen {
@Id
@Column(name="ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="NOMBRE",
nullable = false)
private String nombre;
@Column(name="CATEGORIA",
nullable = false)
private String categoria;
//Getters and setters...
SQL скрипт для таблицы:
CREATE TABLE IMAGEN(
ID INTEGER IDENTITY PRIMARY KEY,
NOMBRE VARCHAR(64) NOT NULL,
CATEGORIA VARCHAR(32)
);
Репозиторий изображений:
@Repository
public interface RepositorioImagen extends JpaRepository<Imagen,Integer> {}
Сервис изображений:
@Service
public class ServicioImagen {
@Autowired RepositorioImagen repo;
public List<Imagen> getAll(){
return repo.findAll();
}
public void saveImage(Imagen im) {
repo.save(im);
}
И контроллер:
@Controller
public class Controlador {
@Autowired private ServicioImagen servicioImagen;
private void someMethod(){
servicioImagen.getAll().forEach(e -> System.out.println(e));
Imagen imagen = new Imagen();
imagen.setNombre("SOMENAME");
servicioImagen.saveImage(imagen);
servicioImagen.getAll().forEach(e -> System.out.println(e));
}
}
Я вставил некоторые записи по умолчанию в таблица, когда я создал базу данных, и метод getAll()
получает их красиво, но когда я save()
новый бин, он не сохраняется в базе данных (getAll()
получает ту же информацию, что и раньше). Я прочитал этот ответ, чтобы включить регистратор, а затем обнаружил, что Hibernate даже фиксирует запрос. Я не знаю, что еще делать, вот журнал вставки:
2020-03-07 18:33:55.611 TRACE 1236 --- [lication Thread] .i.SessionFactoryImpl$SessionBuilderImpl : Opening Hibernate Session. tenant=null, owner=null
2020-03-07 18:33:55.612 TRACE 1236 --- [lication Thread] o.h.s.i.AbstractServiceRegistryImpl : Initializing service [role=org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder]
2020-03-07 18:33:55.612 TRACE 1236 --- [lication Thread] o.h.s.i.AbstractServiceRegistryImpl : Initializing service [role=org.hibernate.stat.spi.StatisticsImplementor]
2020-03-07 18:33:55.612 DEBUG 1236 --- [lication Thread] o.h.stat.internal.StatisticsInitiator : Statistics initialized [enabled=false]
2020-03-07 18:33:55.613 TRACE 1236 --- [lication Thread] org.hibernate.internal.SessionImpl : Opened Session [1956899b-7cc9-4f6a-bb51-5b45abbfed69] at timestamp: 15836024356
2020-03-07 18:33:55.616 DEBUG 1236 --- [lication Thread] o.h.e.t.internal.TransactionImpl : begin
2020-03-07 18:33:55.616 TRACE 1236 --- [lication Thread] j.i.AbstractLogicalConnectionImplementor : Preparing to begin transaction via JDBC Connection.setAutoCommit(false)
2020-03-07 18:33:55.616 TRACE 1236 --- [lication Thread] j.i.AbstractLogicalConnectionImplementor : Transaction begun via JDBC Connection.setAutoCommit(false)
2020-03-07 18:33:55.617 TRACE 1236 --- [lication Thread] cResourceLocalTransactionCoordinatorImpl : ResourceLocalTransactionCoordinatorImpl#afterBeginCallback
2020-03-07 18:33:55.622 TRACE 1236 --- [lication Thread] .i.SessionFactoryImpl$SessionBuilderImpl : Opening Hibernate Session. tenant=null, owner=null
2020-03-07 18:33:55.623 TRACE 1236 --- [lication Thread] org.hibernate.internal.SessionImpl : Opened Session [ac23f2c2-d709-4a5e-a206-68fe3e65bd51] at timestamp: 15836024356
2020-03-07 18:33:55.626 DEBUG 1236 --- [lication Thread] o.h.e.i.DefaultMergeEventListener : EntityCopyObserver strategy: disallow
2020-03-07 18:33:55.629 TRACE 1236 --- [lication Thread] o.hibernate.engine.spi.IdentifierValue : ID unsaved-value: null
2020-03-07 18:33:55.630 TRACE 1236 --- [lication Thread] o.h.e.i.AbstractSaveEventListener : Detached instance of: com.bcadaval.memefinder3020.modelo.beans.Imagen
2020-03-07 18:33:55.630 TRACE 1236 --- [lication Thread] o.h.e.i.DefaultMergeEventListener : Merging detached instance
2020-03-07 18:33:55.639 TRACE 1236 --- [lication Thread] o.h.e.internal.DefaultLoadEventListener : Loading entity: [com.bcadaval.memefinder3020.modelo.beans.Imagen#5]
2020-03-07 18:33:55.639 TRACE 1236 --- [lication Thread] o.h.e.internal.DefaultLoadEventListener : Attempting to resolve: [com.bcadaval.memefinder3020.modelo.beans.Imagen#5]
2020-03-07 18:33:55.639 TRACE 1236 --- [lication Thread] o.h.e.internal.DefaultLoadEventListener : Object not resolved in any cache: [com.bcadaval.memefinder3020.modelo.beans.Imagen#5]
2020-03-07 18:33:55.639 TRACE 1236 --- [lication Thread] o.h.p.entity.AbstractEntityPersister : Fetching entity: [com.bcadaval.memefinder3020.modelo.beans.Imagen#5]
2020-03-07 18:33:55.639 DEBUG 1236 --- [lication Thread] org.hibernate.loader.Loader : Loading entity: [com.bcadaval.memefinder3020.modelo.beans.Imagen#5]
2020-03-07 18:33:55.649 DEBUG 1236 --- [lication Thread] org.hibernate.SQL : select imagen0_.ID as ID1_1_1_, imagen0_.CATEGORIA as CATEGORI2_1_1_, imagen0_.NOMBRE as NOMBRE3_1_1_, etiquetas1_.IDIMAGEN as IDIMAGEN1_2_3_, etiqueta2_.ID as IDETIQUE2_2_3_, etiqueta2_.ID as ID1_0_0_, etiqueta2_.NOMBRE as NOMBRE2_0_0_ from IMAGEN imagen0_ left outer join IMAGEN_ETIQUETA etiquetas1_ on imagen0_.ID=etiquetas1_.IDIMAGEN left outer join ETIQUETA etiqueta2_ on etiquetas1_.IDETIQUETA=etiqueta2_.ID where imagen0_.ID=?
2020-03-07 18:33:55.654 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Registering statement [org.hsqldb.jdbc.JDBCPreparedStatement@2673b10e[sql=[select imagen0_.ID as ID1_1_1_, imagen0_.CATEGORIA as CATEGORI2_1_1_, imagen0_.NOMBRE as NOMBRE3_1_1_, etiquetas1_.IDIMAGEN as IDIMAGEN1_2_3_, etiqueta2_.ID as IDETIQUE2_2_3_, etiqueta2_.ID as ID1_0_0_, etiqueta2_.NOMBRE as NOMBRE2_0_0_ from IMAGEN imagen0_ left outer join IMAGEN_ETIQUETA etiquetas1_ on imagen0_.ID=etiquetas1_.IDIMAGEN left outer join ETIQUETA etiqueta2_ on etiquetas1_.IDETIQUETA=etiqueta2_.ID where imagen0_.ID=?], parameters=[[null]]]]
2020-03-07 18:33:55.655 TRACE 1236 --- [lication Thread] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Registering last query statement [org.hsqldb.jdbc.JDBCPreparedStatement@2673b10e[sql=[select imagen0_.ID as ID1_1_1_, imagen0_.CATEGORIA as CATEGORI2_1_1_, imagen0_.NOMBRE as NOMBRE3_1_1_, etiquetas1_.IDIMAGEN as IDIMAGEN1_2_3_, etiqueta2_.ID as IDETIQUE2_2_3_, etiqueta2_.ID as ID1_0_0_, etiqueta2_.NOMBRE as NOMBRE2_0_0_ from IMAGEN imagen0_ left outer join IMAGEN_ETIQUETA etiquetas1_ on imagen0_.ID=etiquetas1_.IDIMAGEN left outer join ETIQUETA etiqueta2_ on etiquetas1_.IDETIQUETA=etiqueta2_.ID where imagen0_.ID=?], parameters=[[null]]]]
2020-03-07 18:33:55.658 TRACE 1236 --- [lication Thread] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [5]
2020-03-07 18:33:55.659 TRACE 1236 --- [lication Thread] org.hibernate.loader.Loader : Bound [2] parameters total
2020-03-07 18:33:55.661 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Registering result set [org.hsqldb.jdbc.JDBCResultSet@20fd1f60]
2020-03-07 18:33:55.662 TRACE 1236 --- [lication Thread] org.hibernate.loader.Loader : Processing result set
2020-03-07 18:33:55.662 TRACE 1236 --- [lication Thread] org.hibernate.loader.Loader : Done processing result set (0 rows)
2020-03-07 18:33:55.663 TRACE 1236 --- [lication Thread] org.hibernate.loader.Loader : Total objects hydrated: 0
2020-03-07 18:33:55.665 TRACE 1236 --- [lication Thread] o.h.e.loading.internal.LoadContexts : Constructing collection load context for result set [org.hsqldb.jdbc.JDBCResultSet@20fd1f60]
2020-03-07 18:33:55.667 DEBUG 1236 --- [lication Thread] o.h.e.l.internal.CollectionLoadContext : No collections were found in result set for role: com.bcadaval.memefinder3020.modelo.beans.Imagen.etiquetas
2020-03-07 18:33:55.668 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing statement [org.hsqldb.jdbc.JDBCPreparedStatement@2673b10e[sql=[select imagen0_.ID as ID1_1_1_, imagen0_.CATEGORIA as CATEGORI2_1_1_, imagen0_.NOMBRE as NOMBRE3_1_1_, etiquetas1_.IDIMAGEN as IDIMAGEN1_2_3_, etiqueta2_.ID as IDETIQUE2_2_3_, etiqueta2_.ID as ID1_0_0_, etiqueta2_.NOMBRE as NOMBRE2_0_0_ from IMAGEN imagen0_ left outer join IMAGEN_ETIQUETA etiquetas1_ on imagen0_.ID=etiquetas1_.IDIMAGEN left outer join ETIQUETA etiqueta2_ on etiquetas1_.IDETIQUETA=etiqueta2_.ID where imagen0_.ID=?], parameters=[[5]]]]
2020-03-07 18:33:55.668 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Closing result set [org.hsqldb.jdbc.JDBCResultSet@20fd1f60]
2020-03-07 18:33:55.684 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Closing prepared statement [org.hsqldb.jdbc.JDBCPreparedStatement@2673b10e[sql=[select imagen0_.ID as ID1_1_1_, imagen0_.CATEGORIA as CATEGORI2_1_1_, imagen0_.NOMBRE as NOMBRE3_1_1_, etiquetas1_.IDIMAGEN as IDIMAGEN1_2_3_, etiqueta2_.ID as IDETIQUE2_2_3_, etiqueta2_.ID as ID1_0_0_, etiqueta2_.NOMBRE as NOMBRE2_0_0_ from IMAGEN imagen0_ left outer join IMAGEN_ETIQUETA etiquetas1_ on imagen0_.ID=etiquetas1_.IDIMAGEN left outer join ETIQUETA etiqueta2_ on etiquetas1_.IDETIQUETA=etiqueta2_.ID where imagen0_.ID=?], parameters=[[5]]]]
2020-03-07 18:33:55.686 TRACE 1236 --- [lication Thread] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Starting after statement execution processing [ON_CLOSE]
2020-03-07 18:33:55.686 TRACE 1236 --- [lication Thread] o.h.e.i.StatefulPersistenceContext : Initializing non-lazy collections
2020-03-07 18:33:55.686 DEBUG 1236 --- [lication Thread] org.hibernate.loader.Loader : Done entity load
2020-03-07 18:33:55.686 TRACE 1236 --- [lication Thread] o.h.e.i.DefaultMergeEventListener : Merging transient instance
2020-03-07 18:33:55.691 TRACE 1236 --- [lication Thread] org.hibernate.engine.internal.Cascade : Processing cascade ACTION_MERGE for: com.bcadaval.memefinder3020.modelo.beans.Imagen
2020-03-07 18:33:55.691 TRACE 1236 --- [lication Thread] org.hibernate.engine.internal.Cascade : Done processing cascade ACTION_MERGE for: com.bcadaval.memefinder3020.modelo.beans.Imagen
2020-03-07 18:33:55.697 TRACE 1236 --- [lication Thread] o.h.e.i.AbstractSaveEventListener : Saving [com.bcadaval.memefinder3020.modelo.beans.Imagen#<null>]
2020-03-07 18:33:55.706 TRACE 1236 --- [lication Thread] o.hibernate.event.internal.WrapVisitor : Wrapped collection in role: com.bcadaval.memefinder3020.modelo.beans.Imagen.etiquetas
2020-03-07 18:33:55.707 TRACE 1236 --- [lication Thread] org.hibernate.engine.spi.ActionQueue : Adding an EntityIdentityInsertAction for [com.bcadaval.memefinder3020.modelo.beans.Imagen] object
2020-03-07 18:33:55.708 TRACE 1236 --- [lication Thread] org.hibernate.engine.spi.ActionQueue : Adding insert with no non-nullable, transient entities: [EntityIdentityInsertAction[com.bcadaval.memefinder3020.modelo.beans.Imagen#<delayed:1>]]
2020-03-07 18:33:55.708 TRACE 1236 --- [lication Thread] org.hibernate.engine.spi.ActionQueue : Adding resolved non-early insert action.
2020-03-07 18:33:55.711 TRACE 1236 --- [lication Thread] org.hibernate.engine.internal.Cascade : Processing cascade ACTION_MERGE for: com.bcadaval.memefinder3020.modelo.beans.Imagen
2020-03-07 18:33:55.714 TRACE 1236 --- [lication Thread] org.hibernate.engine.internal.Cascade : Cascade ACTION_MERGE for collection: com.bcadaval.memefinder3020.modelo.beans.Imagen.etiquetas
2020-03-07 18:33:55.714 TRACE 1236 --- [lication Thread] org.hibernate.engine.internal.Cascade : Done cascade ACTION_MERGE for collection: com.bcadaval.memefinder3020.modelo.beans.Imagen.etiquetas
2020-03-07 18:33:55.714 TRACE 1236 --- [lication Thread] org.hibernate.engine.internal.Cascade : Done processing cascade ACTION_MERGE for: com.bcadaval.memefinder3020.modelo.beans.Imagen
2020-03-07 18:33:55.715 TRACE 1236 --- [lication Thread] org.hibernate.internal.SessionImpl : Closing session [ac23f2c2-d709-4a5e-a206-68fe3e65bd51]
2020-03-07 18:33:55.715 TRACE 1236 --- [lication Thread] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@53fca5fc]
2020-03-07 18:33:55.715 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2020-03-07 18:33:55.715 TRACE 1236 --- [lication Thread] o.h.r.j.i.LogicalConnectionManagedImpl : Closing logical connection
2020-03-07 18:33:55.715 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2020-03-07 18:33:55.715 TRACE 1236 --- [lication Thread] o.h.r.j.i.LogicalConnectionManagedImpl : Logical connection closed
2020-03-07 18:33:55.720 DEBUG 1236 --- [lication Thread] o.h.e.t.internal.TransactionImpl : committing
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] cResourceLocalTransactionCoordinatorImpl : ResourceLocalTransactionCoordinatorImpl#beforeCompletionCallback
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] org.hibernate.internal.SessionImpl : SessionImpl#beforeTransactionCompletion()
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] org.hibernate.internal.SessionImpl : Automatically flushing session
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] .t.i.SynchronizationRegistryStandardImpl : SynchronizationRegistryStandardImpl.notifySynchronizationsBeforeTransactionCompletion
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] j.i.AbstractLogicalConnectionImplementor : Preparing to commit transaction via JDBC Connection.commit()
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] j.i.AbstractLogicalConnectionImplementor : Transaction committed via JDBC Connection.commit()
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] j.i.AbstractLogicalConnectionImplementor : LogicalConnection#afterTransaction
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] j.i.AbstractLogicalConnectionImplementor : re-enabling auto-commit on JDBC Connection after completion of JDBC-based transaction
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] cResourceLocalTransactionCoordinatorImpl : ResourceLocalTransactionCoordinatorImpl#afterCompletionCallback(true)
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] .t.i.SynchronizationRegistryStandardImpl : SynchronizationRegistryStandardImpl.notifySynchronizationsAfterTransactionCompletion(3)
2020-03-07 18:33:55.721 TRACE 1236 --- [lication Thread] org.hibernate.internal.SessionImpl : SessionImpl#afterTransactionCompletion(successful=true, delayed=false)
2020-03-07 18:33:55.722 TRACE 1236 --- [lication Thread] org.hibernate.internal.SessionImpl : Closing session [1956899b-7cc9-4f6a-bb51-5b45abbfed69]
2020-03-07 18:33:55.722 TRACE 1236 --- [lication Thread] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@4ec81919]
2020-03-07 18:33:55.722 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2020-03-07 18:33:55.722 TRACE 1236 --- [lication Thread] o.h.r.j.i.LogicalConnectionManagedImpl : Closing logical connection
2020-03-07 18:33:55.727 TRACE 1236 --- [lication Thread] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2020-03-07 18:33:55.727 TRACE 1236 --- [lication Thread] o.h.r.j.i.LogicalConnectionManagedImpl : Logical connection closed
Я также заметил, что запрос зафиксирован после закрытия соединения (отметка времени 2020-03-07 18:33:55.720
для Ctrl + F), я не знаю, связано ли это с постоянством.