ORM IN WEB PROGRAMMING Course project report for 6WW Erik Wang
Problems with web programming When people do the web design Design from functional aspects Programmer also needs to understand database Code is not sophisticated This keeps long from CGI, ASP, PHP, JSP Traditional web programming has some problems Low efficiency Hard to maintain Hard to re-use
Evolution of web system CGI JSP Web Service Present layer ASP PHP Present layer Application Business logic layer Functions Library Business Model Entity Business process ORM Database layer DBMS Data layer DBMS Data service Data in memory More abstract observation of business model Various data layer source and present method New database-like-system fit for large scale and fast data computing
ORM Object relation mapping A Framework to do database memory mapping Free developers from database The idea from JAVA EE EJB entity bean May implementations toplink, JPA, active record etc. Hibernate is an implementation of ORM which can be used for Web Application
Data persistent From the software designer s view: You just know the data will be saved, but you don t need to worry who will do it and how they do it Data in memory is not persistent Data in database is persistent From the programmer s aspect Call a persistent method without use SQL query
New method to thinking - MDE Think about the object from the real world - Abstract things, using natural language to describe them - Business analyst and domain expert can describe requirements better
Hibernate Everything Data
Approach by Hibernate Hibernate provides a new method to design the system 1. Design the system from data aspect 2. Generate class from database entity 3. Use DAO to access data 4. Work with other frameworks
Using Hibernate Direct engineering and Reverse engineering From JAVA code to database generate DDL From database to JAVA code generate JAVA CLASS Relationship mapping define in XML or JAVA annotation 2 level of Cache
Case study A news web system Requirements statement A department news web site Publish news to the website - CRUD Highlight News has many reference objects: Image, reviews, related news, etc. Challenges Too many functions to be implemented Data relations are complex
Case study Old idea of design Consider from functional consideration Function 1: Login/Logout - login.php Function 2: Show a list showallnews.php Function 3: Add a news addnews.php Database: table account table news table account_connect_news Peter, Programmer If add a new entry: A series of database queries are required
Case study - Access database Access to database Use JDBC/ODBC Code in page $hostname_cnn1 = "localhost"; $database_cnn1 = "macmovie"; $username_cnn1 = "macmovie"; $password_cnn1 = "macmovie"; $cnn1 = mysql_pconnect($hostname_cnn1, $username_cnn1,$password_cnn1) or trigger_error(mysql_error(),e_user_error); require_once('dbconn.php'); include 'util.php'; mysql_select_db($database_cnn1, $cnn1); No more SQL query Peter, Programmer
Case study Abstract the problem Abstract the factors in the problem Entity News, Images, Comments etc.. Properties Michal, Modeler SN, NAME, CONTENT, DATE Methods News can be: added, posted, audited, modified, removed, etc.. Images can be: added, posted, audited, removed, attached, etc...
Case study - Relation Mapping tdnews.xml <set name="tdaddonimgs" cascade="all" table="tdnewsaddonimgs" > <key column="sn" /> <many-to-many column="isn" unique="true" class="eznews.tdaddonimgs" /> </set> Dave, DB guru Reverse Engineering Peter, Programmer tdaddonimgs.xml <join table="tdnewsaddonimgs" optional="true" inverse="true"> <key column="isn" /> <many-to-one name="news" column="sn" not-null="true" /> </join>
Case study Generate JAVA class Peter, Programmer
Case study Entity to database entity Hibernate do database access works Includes: Mapping data type to db type Generate entity JAVA class or DB DDL Translate JAVA methods to SQL queries Hint: Use connection pool work with Hibernate
Case study example: add news Get a news object and some image objects Add properties to news and image Refer image to the news Get a transaction object from session factory Call SaveorUpdate() What actually happen are: 1. Run a insert query into table news 2. Run some insert queries into table images Peter, Programmer 3. Run some insert queries into middle table news_image
Case study - Following steps Implement methods by JAVA programming Enclosure to a function Servlet Build web page HTML/CSS/JS part Combine Servlet to web page (JSP) Use other JAVA solution to be better (Spring, Structs)
Conclusions ORM provides a new method to design software Advantages: Re-use models from other system Web developers don t need care about database layer Improve the quality of the source codes Disadvantage ORM is not a complete MDE implement Additional layer that increasing potential risk (N+1 problem)
Thank you
Case study Adopting MDE Database design Consider from data flow aspect What kind of data will be composited What are the state changes for the objects Design the system from object aspect Attributions? Functions?
N+1 problem from hibernate