简体   繁体   English

我在休眠时看不到 JPA 中的日志

[英]I can't see logs in JPA with hibernate

I have already been setting property in persistence.xml我已经在persistence.xml中设置了属性

<property name="hibernate.show_sql" value="true" /> 

But!但! when i execute a source which has method to insert SQL and select SQL, I can't see logs about select SQL on console.当我执行具有插入 SQL 和选择 SQL 的方法的源时,我在控制台上看不到有关选择 SQL 的日志。 Just I can see logs about insert SQL.只是我可以看到有关插入 SQL 的日志。

Why?为什么? this is right?这是对的?

public class JpaMain
{

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpabook");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        try{
            tx.begin();
            System.out.println("###########save start#################");
            save(em);
            System.out.println("###########save end#################");
            System.out.println("###########find start#################");
            find(em);
            System.out.println("###########find end#################");

            tx.commit();

        }

        catch(Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
        finally{
            em.close();
        }

        emf.close();
    }

    public static void save(EntityManager em) {
        Product productA = new Product();
        productA.setId("productA");
        productA.setName("pA");
        em.persist(productA);

        Member member1 = new Member();
        member1.setId("member1");
        member1.setUsername("m1");
        member1.getProducts().add(productA);
        em.persist(member1);
    }

    public static void find(EntityManager em) {
        for (Product product : em.find(Member.class, "member1").getProducts()) {
            System.out.println(product.getName());
        }
    }
}

在此处输入图片说明

When you execute find , there are different layers of caching that it will need to go through:当您执行find ,需要经过不同的缓存层:

Persistence Context > Shared Cache > SQL query to the Database持久化上下文 > 共享缓存 > 对数据库的 SQL 查询

Persistence Context can be considered a cache because it keeps references to all the managed entities.持久性上下文可以被视为缓存,因为它保留对所有托管实体的引用。

If the entity exists in the persistence context , the managed instance is returned.如果实体存在于持久上下文中,则返回托管实例。 If it does not exist or no persistence context had yet been associated with the entity manager, the entity manager goes to the factory to see if the shared cache has the entity instance.如果它不存在或者没有持久化上下文与实体管理器相关联,则实体管理器去工厂查看共享缓存是否有实体实例。 If it does, a new entity is created from the shared one and inserted into the persistence context, and the new managed instance is returned to the caller.如果是,则从共享实体创建一个新实体并将其插入到持久性上下文中,并将新的托管实例返回给调用者。 If it is not in the shared cache, an SQL query is generated to select the entity from the database.如果它不在共享缓存中,则会生成SQL 查询以从数据库中选择实体。

In your case, both persist and find operations have been invoked in the same transaction.在您的情况下,已在同一事务中调用了persistfind操作。 So by persisting, your entities will become managed and they will be stored in the Persistence Context.因此,通过持久化,您的实体将被管理并且它们将存储在持久化上下文中。 After which, you executed find method and since your entities are already in the Persistence Context, it will no longer need to execute any SQL queries to the database.之后,您执行了find方法,并且由于您的实体已经在持久化上下文中,它将不再需要对数据库执行任何 SQL 查询。 Entity object located in the Persistence Context will be returned.将返回位于持久上下文中的实体对象。

This should explain why you're not getting any SELECT SQL logged.这应该可以解释为什么您没有记录任何 SELECT SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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