Hibernate---Basic configuration log information SLF and build the log environment

Source: Internet
Author: User
Tags try catch log4j

SLF log interface, implemented with SLF4J NODEP, log4j

Hibernate we generally use log4j, so delete the previously created SLF4J-NOP package in the Hibernate package, add Log4j-1.2.17.jar

Now Hibernate's package has SLF API jar, cannot and log4j automatically match, so in the middle need an interface, then add slf4j-log4j package (1.5.8)

This allows you to see statements such as built-in tables when you run the interface.

The DDL statement comes out:

14:00:20,587  INFO schemaexport:179-exporting generated schema to database14:00:20,588 DEBUG Schemaexport:303-drop Table if exists teacher14:00:20,890 debug Schemaexport:303-drop table if exists student14:00:21,022 debug Schemaexport: 303-create table Teacher (ID integer NOT NULL, name varchar (255), title varchar (255), primary key (ID)) 14:00:21,309 debu G schemaexport:303-create table student (ID integer NOT NULL, name varchar (255), age integer, primary key (ID)) 14:00:21, 530  INFO Schemaexport:196-schema export Complete

 

Because the debug information is too much, only keep the database information can keep the contents of Log4j.properties only the following 2 sentences, the rest are blocked:

Log4j.rootlogger=warn, Stdoutlog4j.logger.org.hibernate.tool.hbm2ddl=debug

The last DDL log information is as follows:

14:13:24,066  INFO schemaexport:154-running hbm2ddl schema export14:13:24,068 DEBUG schemaexport:170-import file No T found:/import.sql14:13:24,068  INFO schemaexport:179-exporting generated schema to database14:13:24,069 DEBUG Sch  Emaexport:303-drop table if exists teacher14:13:24,193 DEBUG schemaexport:303-drop table if exists student14:13:24,279 DEBUG schemaexport:303-create table Teacher (ID integer NOT NULL, name varchar (255), title varchar (255), PRIMARY KEY (i d) 14:13:24,372 DEBUG schemaexport:303-create table student (ID integer NOT NULL, name varchar (255), age integer, Primar  Y key (ID)) 14:13:24,552  INFO Schemaexport:196-schema export Completehibernate:insert into Teacher (name, title, id) VALUES (?,?,?)

  

Building the JUnit Logging environment

Create a new Myjunit package in Preferrence, add the JUnit package, and then right-click Add libary to import your myjunit into the project.

1. Create a Test code package: Project Right-click New---source Folder---Test, which contains the test code, SRC inside is the project code

2. Create a package with the same model as the one above, and then create the unit test for the testing class Teachertest.java, with the following code:

Package Com.bjsxt.hibernate.model;import static Org.junit.assert.*;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.annotationconfiguration;import Org.hibernate.cfg.configuration;import Org.junit.test;public class Teachertest {@Testpublic void Testteachersave () { Teacher T =new Teacher () T.setid (4); T.setname ("WDFD"); T.settitle ("High"); Configuration cfg=new annotationconfiguration (); Sessionfactory sf=cfg.configure (). Buildsessionfactory (); The default is to find Hibernate.cfg.xml, and then produce a connection factory session session =  sf.opensession (); Session.begintransaction (); Session.save (t); Session.gettransaction (). commit (); Session.close (); Sf.close ();}}

It is time-consuming to create sessionfactory in the code above, so consider using Beforeclass and Afterclass:

Package Com.bjsxt.hibernate.model;import static Org.junit.assert.*;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.annotationconfiguration;import Org.hibernate.cfg.configuration;import Org.junit.afterclass;import Org.junit.beforeclass;import org.junit.Test; public class Teachertest {private static sessionfactory sf=null; @BeforeClasspublic static void Beforeclass () {sf=new Annotationconfiguration (). Configure (). Buildsessionfactory ();} @Testpublic void Testteachersave () {Teacher T =new Teacher (); T.setid (4); T.setname ("WDFD"); T.settitle ("High"); Session session =  sf.opensession (); Session.begintransaction (); Session.save (t); Session.gettransaction (). Commit (); Session.close (); Sf.close ();} @AfterClasspublic static void Afterclass () {sf.close ();}}

  

Sometimes the test file will be faulted, untraceable, and need to be in method plus try catch:

@BeforeClasspublic static void Beforeclass () {try {sf=new annotationconfiguration (). Configure (). Buildsessionfactory ( );} catch (Hibernateexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}

Or add a main method, call Beforeclass ();

Hibernate---Basic configuration log information SLF and build the log environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.