BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
BLG 411E – Software Engineering 
Recitation Session 4 
Application Logging and Object-Relational Mapping 
Atakan Aral, Bilge Süheyla Akkoca 
02.12.2014
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Outline 
1 Logging 
Log4j 
2 ORM 
Hibernate 
3 References
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Application Logging 
Introduction 
Definition 
Recording the events which happen during a software 
execution. 
Logging allows to: 
audit important items, 
analyze application use, 
trace errors. 
Performance is the most important consideration.
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Application Logging 
Logging Levels 
OFF No logging 
FATAL Only errors that cause the application to abort 
ERROR Critical errors that prevent the use case from 
continuing 
WARN Other errors and exceptions where processes 
may still continue, i.e. “Current data 
unavailable, using cached values” 
INFO Life cycle events, completion of use cases, i.e. 
“[Who] booked ticket from [Where] to [Where]” 
DEBUG All other (sub) events 
TRACE All method invocations with parameter and 
return values
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Application Logging 
Tips 
Apply logging levels appropriately 
Avoid side effects (no impact on the application’s 
behavior) 
Log for easy reading and easy parsing 
Include both data and description, no magic numbers 
or characters (i.e. &&&) 
Use a logging pattern (i.e. %d{HH:mm:ss.SSS} 
%-5level [%thread][%logger{0}] %m%n) 
Maximize logging for external systems 
Maximize logging when in trouble, not otherwise 
Avoid excessive string concatenation, use parametrized 
logging methods 
Do not log sensitive information
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Log4j 
Introduction 
Definition 
A Java-based logging utility which provides performance 
improvements and advanced filtering. 
import org . apache . logging . l o g 4 j . LogManager ; 
import org . apache . logging . l o g 4 j . Logger ; 
public class HelloWorld { 
pr ivate s t a t i c f i n a l Logger l 
= LogManager . getLogger ( " HelloWorld " ) ; 
public s t a t i c void main ( St r i n g [ ] args ) { 
l . i n f o ( " Hello , World ! " ) ; 
l . e r r o r ( " Mission f a i l e d ! " ) ; 
} 
}
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Object-Relational Mapping 
Definitions 
Persistence Application’s data to outlive the 
applications process, state of objects 
to live beyond the scope of the JVM so 
that the same state is available later 
DMSs Although ODBMSs are emerging, 
RDBMSs are still the most popular 
ones. 
Paradigm mismatch Object models (i.e. Java) and 
relational models (i.e. SQL) do not 
work very well together. See the next 
slide for the details. 
ORM A programming technique for 
converting data between the two 
incompatible type systems
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Object-Relational Mapping 
Object-relational impedance mismatch 
RDBMSs represent data in a tabular format where 
object-oriented languages represent it as an inter-connected 
graph of objects. Mismatch problems: 
Granularity Object model usually has more classes 
than the number of corresponding tables 
in the database. 
Subtypes Inheritance in OOP does not exist in 
RDBMSs. 
Identity OOP supports identity (a == b) and 
equality (a:equals(b)) while RDBMSs only 
support identity (the primary key). 
Association References vs. foreign keys 
Navigation In OOP, navigation is from one association 
to another, walking the object network. 
That is inefficient in RDBMSs, so JOINs 
are used.
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Hibernate 
Introduction 
Definition 
Hibernate is an ORM library for Java that solves object-relational 
impedance mismatch problems by replacing direct 
persistence-related DB access with high-level object 
handling functions. 
Allows developer to concentrate on Java code instead 
of complex SQL statements. 
Reduces the lines of code, makes the system more 
understandable and easier to maintain. 
Abstracts the application away from the underlying SQL 
database and dialect, fosters portability.
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Hibernate 
Hibernate mapping file 
Class–Table 
Property–Id 
Property–Column 
Types and column names are optional. 
<hibernatemapping 
class name= Product  tab l e =PRODUCTS 
 i d name= product Id  column= pid   
generator class= assigned  /  
 / i d  
proper t y name=proName column=pname /  
proper t y name= p r i c e  /  
proper t y name= date  type= timestamp  
column= creat ion_date  /  
 / class 
 / hibernatemapping
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Hibernate 
Hibernate configuration file 
JDBC connection information (connection.driver_class, 
connection.username, connection.password, 
connection.url) 
Number of connections (connection.pool_size) 
SQL variant (dialect) 
DB schema generation (hbm2ddl.auto) 
Mapping persistent classes (mapping)
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Hibernate 
Hibernate configuration file 
hibernatec o n f i g u r a t i o n  
sessionf a c t o r y  
proper t y name= connect ion . d r i v e r _ c l a s s  
oracle . jdbc . d r i v e r . OracleDr iver 
 / proper t y 
proper t y name= connect ion . u r l  
jdbc:oracle: thin:@www . java4s . com:1521:XE 
 / proper t y 
proper t y name= connect ion . user user / proper t y 
proper t y name= connect ion . password  
password 
 / proper t y 
proper t y name= show_sql t rue  / proper t y 
proper t y name= d i a l e c t  
org . hibernate . d i a l e c t . Orac leDialec t 
 / proper t y 
proper t y name= hbm2ddl . auto update / proper t y 
mapping resource= product .hbm. xml  /  
 / sessionf a c t o r y  
 / hibernatec o n f i g u r a t i o n
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Hibernate 
Sample program code 
import org . hibernate .  ; 
import org . hibernate . cfg .  ; 
public class Cl ientForSave { 
public s t a t i c void main ( St r i n g [ ] args ) { 
Conf igurat ion cfg = new Conf igurat ion ( ) ; 
cfg . conf igure (  hibernate . cfg . xml  ) ; 
SessionFactory f a c t o r y = cfg . bui ldSessionFactory ( ) ; 
Session session = f a c t o r y . openSession ( ) ; 
Product p = new Product ( ) ; 
p . setProduct Id ( 1 0 1 ) ; 
p . setProName (  iPhone  ) ; 
p . setPr i ce (25000) ; 
p . setDate (new Date ( ) ) ; 
session . beginTransact ion ( ) ; 
session . save ( p ) ; 
session . getTransact ion ( ) . commit ( ) ; 
session . close ( ) ; 
f a c t o r y . close ( ) ; 
} 
}
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
Hibernate 
Sample program code 
session = sessionFactory . openSession ( ) ; 
session . beginTransact ion ( ) ; 
L i s t r e s u l t = session . createQuery (  from PRODUCTS ) . l i s t ( ) ; 
for ( Product p : ( L i s t Product ) r e s u l t ) { 
System. out . p r i n t l n (  Product (  
+ p . setProduct Id ( ) +  ) :  + p . setProName ( ) ) ; 
} 
session . getTransact ion ( ) . commit ( ) ; 
session . close ( ) ;
BLG 411E 
Software 
Engineering 
Recitation 4 
Logging 
Log4j 
ORM 
Hibernate 
References 
References and Further Reading 
http://www.javacodegeeks.com/2011/01/ 
10-tips-proper-application-logging.html 
http: 
//blogg.kantega.no/logging-in-java-with-users-in-mind/ 
http://logging.apache.org/log4j/2.x/ 
http://www.agiledata.org/essays/impedanceMismatch.html 
http://hibernate.org/orm/ 
http://www.java4s.com/hibernate/ 
hibernate-hello-world-program-saving-an-objcet/

Software Engineering - RS4

  • 1.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References BLG 411E – Software Engineering Recitation Session 4 Application Logging and Object-Relational Mapping Atakan Aral, Bilge Süheyla Akkoca 02.12.2014
  • 2.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Outline 1 Logging Log4j 2 ORM Hibernate 3 References
  • 3.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Application Logging Introduction Definition Recording the events which happen during a software execution. Logging allows to: audit important items, analyze application use, trace errors. Performance is the most important consideration.
  • 4.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Application Logging Logging Levels OFF No logging FATAL Only errors that cause the application to abort ERROR Critical errors that prevent the use case from continuing WARN Other errors and exceptions where processes may still continue, i.e. “Current data unavailable, using cached values” INFO Life cycle events, completion of use cases, i.e. “[Who] booked ticket from [Where] to [Where]” DEBUG All other (sub) events TRACE All method invocations with parameter and return values
  • 5.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Application Logging Tips Apply logging levels appropriately Avoid side effects (no impact on the application’s behavior) Log for easy reading and easy parsing Include both data and description, no magic numbers or characters (i.e. &&&) Use a logging pattern (i.e. %d{HH:mm:ss.SSS} %-5level [%thread][%logger{0}] %m%n) Maximize logging for external systems Maximize logging when in trouble, not otherwise Avoid excessive string concatenation, use parametrized logging methods Do not log sensitive information
  • 6.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Log4j Introduction Definition A Java-based logging utility which provides performance improvements and advanced filtering. import org . apache . logging . l o g 4 j . LogManager ; import org . apache . logging . l o g 4 j . Logger ; public class HelloWorld { pr ivate s t a t i c f i n a l Logger l = LogManager . getLogger ( " HelloWorld " ) ; public s t a t i c void main ( St r i n g [ ] args ) { l . i n f o ( " Hello , World ! " ) ; l . e r r o r ( " Mission f a i l e d ! " ) ; } }
  • 7.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Object-Relational Mapping Definitions Persistence Application’s data to outlive the applications process, state of objects to live beyond the scope of the JVM so that the same state is available later DMSs Although ODBMSs are emerging, RDBMSs are still the most popular ones. Paradigm mismatch Object models (i.e. Java) and relational models (i.e. SQL) do not work very well together. See the next slide for the details. ORM A programming technique for converting data between the two incompatible type systems
  • 8.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Object-Relational Mapping Object-relational impedance mismatch RDBMSs represent data in a tabular format where object-oriented languages represent it as an inter-connected graph of objects. Mismatch problems: Granularity Object model usually has more classes than the number of corresponding tables in the database. Subtypes Inheritance in OOP does not exist in RDBMSs. Identity OOP supports identity (a == b) and equality (a:equals(b)) while RDBMSs only support identity (the primary key). Association References vs. foreign keys Navigation In OOP, navigation is from one association to another, walking the object network. That is inefficient in RDBMSs, so JOINs are used.
  • 9.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Hibernate Introduction Definition Hibernate is an ORM library for Java that solves object-relational impedance mismatch problems by replacing direct persistence-related DB access with high-level object handling functions. Allows developer to concentrate on Java code instead of complex SQL statements. Reduces the lines of code, makes the system more understandable and easier to maintain. Abstracts the application away from the underlying SQL database and dialect, fosters portability.
  • 10.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Hibernate Hibernate mapping file Class–Table Property–Id Property–Column Types and column names are optional. <hibernatemapping class name= Product tab l e =PRODUCTS i d name= product Id column= pid generator class= assigned / / i d proper t y name=proName column=pname / proper t y name= p r i c e / proper t y name= date type= timestamp column= creat ion_date / / class / hibernatemapping
  • 11.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Hibernate Hibernate configuration file JDBC connection information (connection.driver_class, connection.username, connection.password, connection.url) Number of connections (connection.pool_size) SQL variant (dialect) DB schema generation (hbm2ddl.auto) Mapping persistent classes (mapping)
  • 12.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Hibernate Hibernate configuration file hibernatec o n f i g u r a t i o n sessionf a c t o r y proper t y name= connect ion . d r i v e r _ c l a s s oracle . jdbc . d r i v e r . OracleDr iver / proper t y proper t y name= connect ion . u r l jdbc:oracle: thin:@www . java4s . com:1521:XE / proper t y proper t y name= connect ion . user user / proper t y proper t y name= connect ion . password password / proper t y proper t y name= show_sql t rue / proper t y proper t y name= d i a l e c t org . hibernate . d i a l e c t . Orac leDialec t / proper t y proper t y name= hbm2ddl . auto update / proper t y mapping resource= product .hbm. xml / / sessionf a c t o r y / hibernatec o n f i g u r a t i o n
  • 13.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Hibernate Sample program code import org . hibernate . ; import org . hibernate . cfg . ; public class Cl ientForSave { public s t a t i c void main ( St r i n g [ ] args ) { Conf igurat ion cfg = new Conf igurat ion ( ) ; cfg . conf igure ( hibernate . cfg . xml ) ; SessionFactory f a c t o r y = cfg . bui ldSessionFactory ( ) ; Session session = f a c t o r y . openSession ( ) ; Product p = new Product ( ) ; p . setProduct Id ( 1 0 1 ) ; p . setProName ( iPhone ) ; p . setPr i ce (25000) ; p . setDate (new Date ( ) ) ; session . beginTransact ion ( ) ; session . save ( p ) ; session . getTransact ion ( ) . commit ( ) ; session . close ( ) ; f a c t o r y . close ( ) ; } }
  • 14.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References Hibernate Sample program code session = sessionFactory . openSession ( ) ; session . beginTransact ion ( ) ; L i s t r e s u l t = session . createQuery ( from PRODUCTS ) . l i s t ( ) ; for ( Product p : ( L i s t Product ) r e s u l t ) { System. out . p r i n t l n ( Product ( + p . setProduct Id ( ) + ) : + p . setProName ( ) ) ; } session . getTransact ion ( ) . commit ( ) ; session . close ( ) ;
  • 15.
    BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References References and Further Reading http://www.javacodegeeks.com/2011/01/ 10-tips-proper-application-logging.html http: //blogg.kantega.no/logging-in-java-with-users-in-mind/ http://logging.apache.org/log4j/2.x/ http://www.agiledata.org/essays/impedanceMismatch.html http://hibernate.org/orm/ http://www.java4s.com/hibernate/ hibernate-hello-world-program-saving-an-objcet/