1. Home
  2. Docs
  3. Chapter 14. Database Design & Engineering
  4. 4. Programmer’s Guide
  5. How to generate ORM code from Class Diagram

How to generate ORM code from Class Diagram

Download PDF

Once you have designed your object mode in class diagram, you can generate the ORM code from it. This article shows you how to generate ORM code from class diagram.

Generating ORM code from class diagram

  1. Select Tools > Hibernate > Generate Code… from the toolbar. This opens the Database Code Generation window.
    Database Code Generation
  2. Select the (programming) language of your project.
    Selecting language
  3. Specify the output path of the generated code.
    Specifying output path
  4. Configure the code generation options. Read the next sections for details about those available options.
  5. Click OK. The Generate ORM Code/Database dialog box will appear, indicating the progress of code generation. Click Close when complete. If succeed, you can find the generated source files in the folder specified in the Output Path field.

Overview of Database Code Generation

Database Code Generation window

Option Description
Generate Code and Database – generate both ORM code and database.

Code only – generate only ORM code but not database.

Database only – generate only database but not ORM code.

Language The language of your application to be developed. Just leave it as-is if you are uncertain or not going to develop any applications.
Output Path The folder to store the generated DDL files, source files and library files (if any).
Deploy To Specify the template setting for the purpose of the generated code. This setting affects the generated optional Jar and Datasource setting in database connection. You can select Standalone Application, WebLogic Application Server 8.1/9.0, WebSphere Application Server Community Edition 1.0, JBoss Application Server and Generic Application server.
Framework Select between Hibernate XML or JPA.
Error Handling
  • Return false/null – It returns false/null in the method to terminate its execution
  • Throw PersistentExpcetion – It throws a PersistentException which will be handled by the caller
  • Throw RuntimeException – It throws a RuntimeException which will be handled by the caller
  • Throw User Defined Exception – Choose this option to manually specify an exception class
Logging Select how to log exceptions, either do not show, print to error stream or print to log4j.
Default Lazy Collection Initialization Lazy initialization avoids objects in collection from being loaded unnecessarily when the main object is being loaded.
Default Lazy Association Initialization Lazy initialization avoids associated objects from being loaded unnecessarily when the main object is being loaded.
Association Handling Select the type of association handling to be used, either Smart or Standard.

  • Smart – When you update on end of a bi-directional association, the generated persistent code is able to update the other end automatically. Besides, you do not need to cast the retrieved object(s) into its corresponding persistence class when retrieving object(s) from the collection.
  • Standard – You must update both ends of a bi-directional association manually to maintain the consistency of association. Besides, casting of object(s) to its corresponding persistence class is required when retrieving object(s) from the collection.
Persistent API Select the type of persistent code to be generated, either Static Methods, Factor Class, DAO, POJO or Mapping Only.
Generate Criteria Check this option to generate the criteria class for each ORM-Persistable class. The criteria class supports querying the database by specifying the searching criteria.
Serializable
Cache Options Click to choose a cache option.
Select Optional Jar Click on the Select Optional Jar button, then select the libraries and JDBC driver to be included in the generation of the orm.jar file.
Advanced Settings Click to specify the advanced settings.
Samples Samples files, including Java application, servlet, servlet filter and Java Server Page (JSP) sample are available for generation. The generated sample files guide you through the usage of the Java persistence class. You can check the options to generate the sample files for reference.
Scripts Check the options to generate the scripts, including Ant File, Batch and Shell Script which allow you to execute the scripts directly.
Generate Filter and Web Application Descriptor (web.xml) Check this option to generate filter and web application description, which are useful in web application development.