Session.save () method in hibernate

session.save (c) converts instantaneous objects into persistent objects, but the c object does not have a primary key. A primary key generation strategy is configured in the configuration file. When native, executes session.save (c), an insert statement is issued. When you hit a breakpoint in the commit method, you can see that id has a value at this time, but there is no data in the database at this time. How does c in session cache have id?

@Test
public void test2(){
    Configuration conf = new Configuration().configure();
    SessionFactory sessionFactory = conf.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    Customer c = new Customer();
    c.setCust_name("");
    session.save(c);
    
    tx.commit();
    session.close();
}

Mar.04,2021

generate the source code of Id

protected Serializable saveWithGeneratedId(
            Object entity,
            String entityName,
            Object anything,
            EventSource source,
            boolean requiresImmediateIdAccess) {
        EntityPersister persister = source.getEntityPersister( entityName, entity );
        Serializable generatedId = persister.getIdentifierGenerator().generate( source, entity );//generatedIdId
        if ( generatedId == null ) {
            throw new IdentifierGenerationException( "null id generated for:" + entity.getClass() );
        }
        else if ( generatedId == IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR ) {
            return source.getIdentifier( entity );
        }
        else if ( generatedId == IdentifierGeneratorHelper.POST_INSERT_INDICATOR ) {
            return performSave( entity, null, persister, true, anything, source, requiresImmediateIdAccess );
        }
        else {

            if ( log.isDebugEnabled() ) {
                log.debug(
                        "generated identifier: " +
                                persister.getIdentifierType().toLoggableString( generatedId, source.getFactory() ) +
                                ", using strategy: " +
                                persister.getIdentifierGenerator().getClass().getName()
                        //TODO: define toString()s for generators
                );
            }

            return performSave( entity, generatedId, persister, false, anything, source, true );
        }
    }

if the insert executes successfully, it will return id,. At this time, there is no commit, so you can't see it in the database

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3146b-2bd76.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3146b-2bd76.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?