Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

In Hibernate session.persist doesn't work, but session.save does. Why? Here is the error…

  1. User

    In Hibernate session.persist doesn't work, but session.save does. Why?

    Here is the error or set of errors I get.: cd /home/user/NetBeansProjects/MyApp; JAVA_HOME=/app/jdk /app/netbeans/java/maven/bin/mvn -Dexec.vmArgs= "-Dexec.args=exec.vmArgs−classpath{exec.vmArgs} -classpath %classpath {exec.mainClass} ${exec.appArgs}" -Dexec.executable=/app/jdk/bin/java -Dexec.mainClass=com.mycompany.MyApp.Driver -Dexec.classpathScope=runtime -Dexec.appArgs= process-classes org.codehaus.mojo:exec-maven-plugin:3.1.0:exec Scanning for projects...

    ---------------< com.mycompany:MyApp >--------------- Building MyApp 1.0-SNAPSHOT from pom.xml --------------------------------[ jar ]---------------------------------

    --- resources:3.3.1:resources (default-resources) @ MyApp --- Copying 1 resource from src/main/resources to target/classes

    --- compiler:3.8.1:compile (default-compile) @ MyApp --- Nothing to compile - all classes are up to date

    --- exec:3.1.0:exec (default-cli) @ MyApp --- Sep. 11, 2023 8:57:31 P.M. org.hibernate.Version logVersion INFO: HHH000412: Hibernate ORM core version 6.3.0.Final Sep. 11, 2023 8:57:32 P.M. org.hibernate.cache.internal.RegionFactoryInitiator initiateService INFO: HHH000026: Second-level cache disabled Sep. 11, 2023 8:57:32 P.M. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using built-in connection pool (not intended for production use) Sep. 11, 2023 8:57:32 P.M. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: Loaded JDBC driver class: org.sqlite.JDBC Sep. 11, 2023 8:57:32 P.M. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001012: Connecting with JDBC URL [jdbc:sqlite:investment-tracking.sqlite] Sep. 11, 2023 8:57:32 P.M. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {} Sep. 11, 2023 8:57:32 P.M. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false Sep. 11, 2023 8:57:32 P.M. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplPooledConnections<init>INFO:HHH10001115:Connectionpoolsize:20(min=1)Sep.11,20238:57:34P.M.org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiatorinitiateServiceINFO:HHH000489:NoJTAplatformavailable(set′hibernate.transaction.jta.platform′toenableJTAplatformintegration)Sep.11,20238:57:34P.M.org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImplgetIsolatedConnectionINFO:HHH10001501:ConnectionobtainedfromJdbcConnectionAccess[org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiatorPooledConnections <init> INFO: HHH10001115: Connection pool size: 20 (min=1) Sep. 11, 2023 8:57:34 P.M. org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService INFO: HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) Sep. 11, 2023 8:57:34 P.M. org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiatorConnectionProviderJdbcConnectionAccess@6a6c7f42] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. Exception in thread "main" java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@2d5a1588 is closed at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:37) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:142) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:273) at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.rollback(AbstractLogicalConnectionImplementor.java:121) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImplTransactionDriverControlImpl.rollback(JdbcResourceLocalTransactionCoordinatorImpl.java:290)atorg.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:142)atcom.mycompany.MyApp.Driver.main(Driver.java:56)Commandexecutionfailed.org.apache.commons.exec.ExecuteException:Processexitedwithanerror:1(Exitvalue:1)atorg.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)atorg.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)atorg.codehaus.mojo.exec.ExecMojo.executeCommandLine(ExecMojo.java:1000)atorg.codehaus.mojo.exec.ExecMojo.executeCommandLine(ExecMojo.java:947)atorg.codehaus.mojo.exec.ExecMojo.execute(ExecMojo.java:471)atorg.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:126)atorg.apache.maven.lifecycle.internal.MojoExecutor.doExecute2(MojoExecutor.java:328)atorg.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:316)atorg.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)atorg.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:174)atorg.apache.maven.lifecycle.internal.MojoExecutor.accessTransactionDriverControlImpl.rollback(JdbcResourceLocalTransactionCoordinatorImpl.java:290) at org.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:142) at com.mycompany.MyApp.Driver.main(Driver.java:56) Command execution failed. org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404) at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166) at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:1000) at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:947) at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:471) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174) at org.apache.maven.lifecycle.internal.MojoExecutor.access000 (MojoExecutor.java:75) at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162) at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283) at org.apache.maven.cli.MavenCli.main (MavenCli.java:206) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:566) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)

    BUILD FAILURE

    Total time: 6.350 s Finished at: 2023-09-11T20:57:34-04:00

    Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.1.0:exec (default-cli) on project MyApp: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

    To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.

    For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

  2. Assistant

    The error message "java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@2d5a1588 is closed" suggests that the error is related to the state of the Hibernate session's logical connection.

    Both session.persist() and session.save() methods are used to persist an entity to the database. However, they have a subtle difference in behavior, which might explain the error you are encountering.

    session.persist() is used to make a transient instance persistent, meaning that it associates the entity with the Hibernate session and schedules an insert operation to be executed when the session is flushed. When you call session.persist(), Hibernate assumes that the entity is not yet associated with the database.

    On the other hand, session.save() is used to persist a transient or detached instance to the database. It also associates the entity with the Hibernate session and schedules an insert operation to be executed when the session is flushed. However, session.save() returns the generated identifier of the entity, whereas session.persist() does not.

    The error you are encountering could be due to the state of the entity or the session. It's possible that the entity you are trying to persist using session.persist() is already associated with the database and the session is closed or in an inconsistent state.

    To resolve the issue, you can try the following steps:

    1. Ensure that the entity you are trying to persist is not already associated with the database. If it has a primary key value, Hibernate might consider it as a detached entity, and session.persist() will throw an exception.

    2. Check the state of the Hibernate session before calling session.persist(). Ensure that the session is open and in a consistent state.

    3. If the entity is already persistent and you want to update it, you can use session.merge() instead of session.persist(). session.merge() will associate the entity with the session and perform an insert or update operation based on its state.

    It's worth noting that session.save() is considered more forgiving because it can handle both transient and detached instances. However, it's recommended to use session.persist() for persisting new entities and session.merge() for updating or persisting existing entities to ensure consistent behavior.

  3. User

    Redo the answer you just made, but this time have it be concise.

  4. Assistant

    The error "java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@2d5a1588 is closed" indicates a problem with the Hibernate session's logical connection being closed.

    The difference between session.persist() and session.save() is that session.persist() is used for persisting new entities, while session.save() can handle both new and existing entities.

    To resolve the issue, ensure that the entity is not already associated with the database and check the state of the Hibernate session before calling session.persist(). Alternatively, you can use session.merge() for updating or persisting existing entities.

  5. User

    What's the non-deprecated version of save(), then? Be concise.

  6. Assistant

    The non-deprecated version of session.save() in Hibernate is session.saveOrUpdate().

  7. User

    So, persist is not the non-deprecated version of save(), then, right?

  8. Assistant

    That's correct. session.persist() and session.save() are two separate methods in Hibernate with different behavior. session.persist() is not the non-deprecated version of session.save(). The non-deprecated version of session.save() is session.saveOrUpdate().

  9. User

    Are you sure?

    From what I see in the Hibernate Javadoc, it says the following.: Object save​(Object object) Deprecated. use persist(Object) Object save​(String entityName, Object object) Deprecated. use persist(String, Object) void saveOrUpdate​(Object object) Deprecated. use merge(String, Object) or persist(Object) void saveOrUpdate​(String entityName, Object object) Deprecated. use merge(String, Object) or persist(String, Object)

  10. Assistant

    Apologies for the confusion. You are correct. According to the Hibernate Javadoc, both session.save() and session.saveOrUpdate() methods are marked as deprecated, and it recommends using session.persist() or session.merge() instead. Therefore, session.persist() is the suggested method to use for persisting new entities, while session.merge() should be used for updating or persisting existing entities.

  11. User

    Can session.merge() be used for new entities too?

  12. Assistant

    Yes, session.merge() can be used for both new and existing entities in Hibernate.

    When you use session.merge() for a new entity, it will persist the entity and assign it a new identifier if it doesn't already have one. If the entity already has an identifier, session.merge() will update the corresponding record in the database.

    So, session.merge() offers more flexibility by handling both new and existing entities during the persistence process.

  13. User

    But, session.persist() only handles new entities during the persistence process?

  14. Assistant

    Yes, that's correct.

    The session.persist() method in Hibernate is specifically designed for persisting new entities during the persistence process. It associates the entity with the Hibernate session and schedules an insert operation to be executed when the session is flushed.

    On the other hand, session.merge() can handle both new and existing entities. When you use session.merge() with a new entity, it will persist the entity and assign it a new identifier if needed. For existing entities, session.merge() will update the corresponding record in the database.

    In summary, session.persist() is primarily intended for new entity persistence, while session.merge() can handle both new and existing entities.

  15. User

    Give me examples of the corresponding SQL statements for merge vs persist. Be concise.

  16. Assistant

    The corresponding SQL statements for session.persist() and session.merge() in Hibernate are as follows:

    • session.persist() generates an INSERT statement to persist a new entity.
    • session.merge() generates an INSERT statement for a new entity without an identifier and an UPDATE statement for an existing entity with an identifier.

    In summary:

    • session.persist() generates INSERT statements.
    • session.merge() generates INSERT and UPDATE statements.