Hibernate showing SQL logs only when object.toString() method is called

Solution for Hibernate showing SQL logs only when object.toString() method is called
is Given Below:

Trying my hands at hibernate first level cache examples. As mentioned in the question heading, the Hibernate is showing SQL logs only when I am using toString() method to print the object info. Otherwise no logs. Why is this so?

hibernate.cfg.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb1</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping class="mypack.Table1"/> 
    </session-factory>
</hibernate-configuration>

Table1.java(The entity):

package mypack;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Table1 {
    
    @Id
    int number;

    String name, location;

    public Table1() {
        super();
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Table1 [number=" + number + ", name=" + name + ", location=" + location + "]";
    }
    
}

The Model class(where hibernate is used):

package mypack;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

public class Model
{
    @SuppressWarnings("deprecation")
    public List<Table1> getAll()
    {
        Configuration cfg = new Configuration();
        SessionFactory sessionf= cfg.configure("hibernate.cfg.xml").buildSessionFactory();
        Session session = sessionf.openSession();
        Transaction transact = session.beginTransaction();
        
        Query<?> getall = session.createQuery("from Table1");
        
        
        Table1 one = (Table1)session.load(Table1.class, new Integer(1));
        System.out.println("nrow 1 loaded. : "+one.toString()+"n");
        
        Table1 two = (Table1)session.load(Table1.class, new Integer(2));
        System.out.println("nrow 2 loaded."+two.toString()+"n");
        
        
        System.out.println("n session.evict() not called. Does session contain one ? "+session.contains(one)+"n");
        one = (Table1)session.load(Table1.class, new Integer(1));
        System.out.println("nrow 1 loaded. AGAIN."+one.toString()+"n");
        
        Table1 three = (Table1)session.load(Table1.class, new Integer(3));
        System.out.println("nrow 3 loaded."+three.toString()+"n");
        
        Table1 four = (Table1)session.load(Table1.class, new Integer(4));
        System.out.println("nrow 4 loaded."+four.toString()+"n");
        
        Table1 five = (Table1)session.load(Table1.class, new Integer(5));
        System.out.println("nrow 5 loaded."+five.toString()+"n");
        
        //System.out.println("rows 3,4,5 loaded.");
        
        session.clear();
        System.out.println("session.clear() called"+"n");
        
        one = (Table1)session.load(Table1.class, new Integer(1));
        two = (Table1)session.load(Table1.class, new Integer(2));
        three = (Table1)session.load(Table1.class, new Integer(3));
        four = (Table1)session.load(Table1.class, new Integer(4));
        five = (Table1)session.load(Table1.class, new Integer(5));
        
        System.out.println("ALL ENTITIES LOADED AGAIN."+"n");
        
         List<Table1> returnThis=(List<Table1>)getall.list();
        
        transact.commit();
        
        return returnThis;
    }
}

The List is being returned coz this function is being called from a Servlet which returns this list. The servlet code is not pasted here becoz it doesnt concern here as there is no issue with the HTTP response, just the SQL logging in the Model class.

For Table1 references one to five, as in the code, if I use the toString() method then only the SQL queries are visible in the Eclipse console, otherwise nothing. As you can see, after using session.clear(), when I am loading the Table1 references one to five again, and as I have not used any sop(toString()) statement, it would not show any SQL logs. If I remove the initial sop(toString()) too, then those logs also disappear.

Am I missing something?

Eclipse console screenshot:

enter image description here

Config log4j in your project then add this in your application.properties

    log4j.category.org.springframework.jdbc.core = DEBUG
    log4j.logger.org.hibernate.SQL=debug
    log4j.logger.org.hibernate.type=trace
    logging.level.org.hibernate.SQL=DEBUG
    logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

Now your logs look like this :

2021-08-03 21:14:38.287 DEBUG 14912 --- [enerContainer-1] org.hibernate.SQL                        : select trigcondit0_.id as id1_67_0_, trigcondit0_.created_at as created_2_67_0_, trigcondit0_.logic_block_instance_id as logic_bl5_67_0_, trigcondit0_.result as result3_67_0_, trigcondit0_.state as state4_67_0_, trigcondit0_.triggering_condition_id as triggeri6_67_0_ from trig_condition_instance trigcondit0_ where trigcondit0_.id=?
2021-08-03 21:14:38.288 TRACE 14912 --- [enerContainer-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [INTEGER] - [3722]
2021-08-03 21:14:38.289 DEBUG 14912 --- [enerContainer-1] org.hibernate.SQL                        : update trig_condition_instance set created_at=?, logic_block_instance_id=?, result=?, state=?, triggering_condition_id=? where id=?
2021-08-03 21:14:38.290 TRACE 14912 --- [enerContainer-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [Tue Aug 03 21:14:38 PKT 2021]
2021-08-03 21:14:38.291 TRACE 14912 --- [enerContainer-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [INTEGER] - [3725]
2021-08-03 21:14:38.291 TRACE 14912 --- [enerContainer-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [BOOLEAN] - [false]
2021-08-03 21:14:38.292 TRACE 14912 --- [enerContainer-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [INTEGER] - [1]
2021-08-03 21:14:38.292 TRACE 14912 --- [enerContainer-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [INTEGER] - [53]
2021-08-03 21:14:38.293 TRACE 14912 --- [enerContainer-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [INTEGER] - [3722]
2021-08-03 21:14:38.294  INFO 14912 --- [enerContainer-1] c.w.s.i.TrigConditionInstanceServiceImpl : ## TrigConditionInstance->Save() Processed ##
2021-08-03 21:14:38.295  INFO 14912 --- [enerContainer-1] c.c.w.s.i.WorkflowInstanceServiceImpl    : ## WorkflowInstanceServiceImpl.eventHandleConsumer Processed ##

have you tried <property name="show_sql">true</property> instead <property name="hibernate.show_sql">true</property> ?