数据库:
表映射的3个类
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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost/test </property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider </property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- <mapping class="com.hibernate.entity.Husband" /> <mapping class="com.hibernate.entity.Wife" /> =================================================== <mapping class="com.hibernate.entity.Teacher" /> <mapping class="com.hibernate.entity.Student" /> <mapping class="com.hibernate.entity.Tgroup" /> <mapping class="com.hibernate.entity.Tuser" /> --> <mapping class="com.hibernate.entity.TableC" /> <mapping class="com.hibernate.entity.TableB" /> <mapping class="com.hibernate.entity.TableA" /> </session-factory> </hibernate-configuration>
log4j.properties
### set log levels ### log4j.rootLogger=warn, stdout, D, E ### 输出到控制台 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c:%L - %m%n ### 输出到日志文件 ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = ${WORKDIR}/logs/log.log log4j.appender.D.Append = true ## 输出DEBUG级别以上的日志 log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r] - %c:%L - [%p] %m%n ### 保存异常信息到单独文件 ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender ## 异常日志文件名 log4j.appender.E.File = ${WORKDIR}/logs/error.log log4j.appender.E.Append = true ## 只输出ERROR级别以上的日志!!! log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss} [%t\:%r] - %c\:%L - [%p] %m%n
测试代码: