我正试图在Hibernate 4上增加任何先前版本的Hibernate(或其他ORM)的经验。这非常令人沮丧。我还没有找到明确列出并解释教程项目工作所需的每个步骤的教程或书籍。我也不熟悉Maven。
我通过尝试各种教程的各个部分来完成一个非常简单的项目。到目前为止,我要做的就是打印出从表中加载的对象,其中POJO使用注释绑定到表。
我的代码有效,但是大量的日志消息使控制台混乱。我想关闭此注销(至少在我需要调试问题之前)或将此输出重定向到文件。
目前,我已经尝试了几种方法来关闭我在StackOverflow和其他网站上找到的日志记录。什么都行不通(其中一个“修复”会导致Maven错误)。 POM,程序化和Log4j.properties修复程序对日志的大小没有任何影响。
这是我的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sadakatsu</groupId>
<artifactId>all</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>all</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<!-- <version>2.3</version> -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>7.5.4.v20111024</version>
<destFileName>jetty-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这是我的hibernate.cfg.xml
:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/tickettoride
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
<!-- redacted -->
</property>
<!-- List of XML mapping files -->
<mapping class="com.sadakatsu.tickettoride.orm.City" />
</session-factory>
</hibernate-configuration>
这是我的log4j.properties
文件:
log4j.logger.org.hibernate=ERROR
以下是我的代码的核心:
package com.sadakatsu.main;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import com.sadakatsu.tickettoride.orm.City;
public class HibernateDemo {
private SessionFactory factory;
private ServiceRegistry registry;
private HibernateDemo() {
Configuration configuration = new Configuration();
configuration.configure();
registry = (
new ServiceRegistryBuilder().applySettings(
configuration.getProperties()
).buildServiceRegistry()
);
factory = configuration.buildSessionFactory(registry);
}
public SessionFactory getFactory() {
return factory;
}
public void shutdown() {
factory.close();
}
public static void main(String[] args) {
HibernateDemo hd = new HibernateDemo();
Session session = hd.getFactory().openSession();
Query q = session.createQuery("from City");
@SuppressWarnings("unchecked")
List<City> cities = (List<City>) q.list();
System.out.println("Cities:");
for (City city : cities) {
System.out.format(" - %s\n", city);
}
}
}
这是我的预期输出:
Cities:
- Atlanta
- Boston
- Calgary
- Charleston
- Chicago
- Dallas
- Denver
- Duluth
- El Paso
- Helena
- Houston
- Kansas City
- Las Vegas
- Little Rock
- Los Angeles
- Miami
- Montréal
- Nashville
- New Orleans
- New York
- Oklahoma City
- Omaha
- Phoenix
- Pittsburgh
- Portland
- Raleigh
- Saint Louis
- Salt Lake City
- San Francisco
- Santa Fe
- Sault St. Marie
- Seattle
- Toronto
- Vancouver
- Washington
- Winnipeg
这就是我得到的:
22:52:26.254 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration boolean -> org.hibernate.type.BooleanType@ba4d54
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration boolean -> org.hibernate.type.BooleanType@ba4d54
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration java.lang.Boolean -> org.hibernate.type.BooleanType@ba4d54
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration numeric_boolean -> org.hibernate.type.NumericBooleanType@6f79caec
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration true_false -> org.hibernate.type.TrueFalseType@5c0369c4
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration yes_no -> org.hibernate.type.YesNoType@17ed40e0
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration byte -> org.hibernate.type.ByteType@3930015a
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration byte -> org.hibernate.type.ByteType@3930015a
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration java.lang.Byte -> org.hibernate.type.ByteType@3930015a
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration character -> org.hibernate.type.CharacterType@224aed64
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration char -> org.hibernate.type.CharacterType@224aed64
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration java.lang.Character -> org.hibernate.type.CharacterType@224aed64
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration short -> org.hibernate.type.ShortType@75412c2f
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration short -> org.hibernate.type.ShortType@75412c2f
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration java.lang.Short -> org.hibernate.type.ShortType@75412c2f
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration integer -> org.hibernate.type.IntegerType@483bf400
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration int -> org.hibernate.type.IntegerType@483bf400
22:52:26.270 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration java.lang.Integer -> org.hibernate.type.IntegerType@483bf400
22:52:26.285 [main] DEBUG org.hibernate.type.BasicTypeRegistry - Adding type registration long -> org.hibernate.type.LongType@2cfb4a64
<!-- several hundred more lines deleted for space -->
Cities:
- Atlanta
- Boston
- Calgary
- Charleston
- Chicago
- Dallas
- Denver
- Duluth
- El Paso
- Helena
- Houston
- Kansas City
- Las Vegas
- Little Rock
- Los Angeles
- Miami
- Montréal
- Nashville
- New Orleans
- New York
- Oklahoma City
- Omaha
- Phoenix
- Pittsburgh
- Portland
- Raleigh
- Saint Louis
- Salt Lake City
- San Francisco
- Santa Fe
- Sault St. Marie
- Seattle
- Toronto
- Vancouver
- Washington
- Winnipeg
有人可以向这个绝对的初学者解释如何完全关闭日志记录或将输出重定向到文件?这两种方法都会让我前进。
如有必要,我可以在whole project in question找到整个项目的干净版本。
答案 0 :(得分:0)
log4j.properties
中打开名为src/main/resources
的文件。log4j.logger.org.hibernate=ERROR
并保存文件。这将关闭由Hibernate类打印出的所有调试日志。如果您在任何时候需要有关Hibernate的详细信息,请将该行更改为log4j.logger.org.hibernate=DEBUG
。
否则,请发布更多信息,例如log4j.properties
。
编辑:根据您发布的源代码,我可以看到您的应用程序正在使用Logback作为主要日志记录框架。由于依赖spring-boot-starter-web
,导入了Logback。 Logback是原始Log4J框架的后续版本,也是SLF4J API的一个实现,它提供了一个桥接,可以将所有日志消息从Log4J API路由到SLF4J API。这就是您的Log4J配置被忽略的原因。
您需要在logback.xml
下创建名为src/main/resources
的文件,并向其添加以下内容:
<configuration>
<logger name="org.hibernate" level="ERROR"/>
</configuration>