P6Spy 不使用 HSQLDB 记录休眠更新
P6Spy does not log hibernate update with HSQLDB
我正在尝试使用 HSQLDB 建立一个简单的项目来解释 Hibernate 的基础知识。
为了更好地理解 Hibernate 内部,我想安装 P6Spy 以显示相应的 SQL 语句。
我无法在我的控制台中获得 SQL 更新语句。
Hibernate: insert into User (id, name) values (null, ?)
1505546078019|0|statement|connection 0|insert into User (id, name) values (null, ?)|insert into User (id, name) values (null, 'A')
Hibernate: update User set name=? where id=?
1505546078027|0|commit|connection 0||
P6Spy and Hibernate show me the insert statement, but only Hibernate displays the update statement
这是我的源代码:
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="connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property>
<property name="connection.url">jdbc:p6spy:hsqldb:mem:so</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
com/so/User.java
package com.so;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column
private String name;
public User() {
}
public Long getId() {
return id;
}
protected void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
com/so/Main
package com.so;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Main {
private static final SessionFactory sessionFactory = buildSessionFactory();
public static void main(String[] args) {
sessionFactory.getCurrentSession().beginTransaction();
User e = new User();
e.setName("A");
sessionFactory.getCurrentSession().save(e);
e.setName("B");
sessionFactory.getCurrentSession().getTransaction().commit();
}
private static SessionFactory buildSessionFactory() {
try {
return new AnnotationConfiguration()
.addAnnotatedClass(User.class)
.configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.so</groupId>
<artifactId>test-hibernate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
<!-- Hibernate uses slf4j for logging, for our purposes here use the simple
backend -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.9.0.GA</version>
</dependency>
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>
spy.properties
appender=com.p6spy.engine.spy.appender.StdoutLogger
我遇到了同样的问题。对我来说,这是因为 hibernate 将更新添加到批处理中,并且默认情况下批处理调用不会记录在 p6spy 中。更新 p6spy 属性,以便记录批处理。
spy.properties
#list of categories to exclude: error, info, batch, debug, statement,
#commit, rollback and result are valid values
#excludecategories=
excludecategories=info,debug,result
我正在尝试使用 HSQLDB 建立一个简单的项目来解释 Hibernate 的基础知识。 为了更好地理解 Hibernate 内部,我想安装 P6Spy 以显示相应的 SQL 语句。
我无法在我的控制台中获得 SQL 更新语句。
Hibernate: insert into User (id, name) values (null, ?)
1505546078019|0|statement|connection 0|insert into User (id, name) values (null, ?)|insert into User (id, name) values (null, 'A')
Hibernate: update User set name=? where id=?
1505546078027|0|commit|connection 0||
P6Spy and Hibernate show me the insert statement, but only Hibernate displays the update statement
这是我的源代码:
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="connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property>
<property name="connection.url">jdbc:p6spy:hsqldb:mem:so</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
com/so/User.java
package com.so;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column
private String name;
public User() {
}
public Long getId() {
return id;
}
protected void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
com/so/Main
package com.so;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Main {
private static final SessionFactory sessionFactory = buildSessionFactory();
public static void main(String[] args) {
sessionFactory.getCurrentSession().beginTransaction();
User e = new User();
e.setName("A");
sessionFactory.getCurrentSession().save(e);
e.setName("B");
sessionFactory.getCurrentSession().getTransaction().commit();
}
private static SessionFactory buildSessionFactory() {
try {
return new AnnotationConfiguration()
.addAnnotatedClass(User.class)
.configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.so</groupId>
<artifactId>test-hibernate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
<!-- Hibernate uses slf4j for logging, for our purposes here use the simple
backend -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.9.0.GA</version>
</dependency>
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>
spy.properties
appender=com.p6spy.engine.spy.appender.StdoutLogger
我遇到了同样的问题。对我来说,这是因为 hibernate 将更新添加到批处理中,并且默认情况下批处理调用不会记录在 p6spy 中。更新 p6spy 属性,以便记录批处理。
spy.properties
#list of categories to exclude: error, info, batch, debug, statement,
#commit, rollback and result are valid values
#excludecategories=
excludecategories=info,debug,result