Jump to Menu

Define custom implementations for ORM Persistable class

Hibernate is one of the most popular Object Relational Mapping (ORM) framework on the market which helps in map Java objects with relational database in object-oriented programming. With Visual Paradigm you can generate Hibernate source code from UML class models and Entity Relationship model for building database applications. For most of the case the generated Hibernate source code are just used out-of-the box as the persistence layer and developers can building business logic for their applications on top of it. But in some case developers may want to add simple business logic to the generated persistence layer to ease their job. To do this we can extend the functionality of the persistence layer by defining custom implementations. In this tutorial, we will show you step-by-step procedures to define custom implementation for ORM classes. Visual Paradigm, Eclipse Mars.1 and Microsoft SQL Server will be used in this tutorial.

Compatible edition(s): Enterprise, Professional, Standard

  • March 01, 2016
  • Views: 32,687
  • PDF

We assume you already have Visual Paradigm Standard installed and integrated with Eclipse. SQL Server is also being setup and ready to use. Suppose we have a simple ORM class, Product, and we want to define extra features for calculate the product price after volume discount:

  1. Click on the Product class and drag out the resource icon.
    Click resource
  2. Release the mouse button on the desire location in the diagram.
  3. Select Create ORM Implementation Class in Resource Catalog..
    Create ORM Implementation Class
  4. Name the created class as ProductImpl.
    Name ORM Implementation Class
  5. Right-click on ProductImpl and select Add > Operation.
    Add Operation
  6. Name the operation as getVolumeDiscountPrice(qty : int) : float
    Define Operation
  7. Now the model is ready and we can proceed to generate ER model and hibernate code. First we define the default database for our project. Select Modeling > ORM > Database Configuration...
    Database Configuration
  8. Select SQL Server as our default database. Specify the connection details to the SQL Server. Use the Test Connection button to make sure your configuration detail works. Then press OK to close the Database Configuration dialog.
    Test Connection
  9. Right-click on the blank area of the class diagram and select Synchronize to Entity Relationship Diagram, follow the wizard to perform the synchronization with default settings.
    Synchronize to Entity Relationship Diagram
  10. The ERD for our model is generated. To simplify the testing we can predefine some sample data for our database. Right-click on the blank area of the ERD and select Show Table Record Editor or View Editor.
    Open Record Editor
  11. Enter the sample record below to the Table Record Editor.
    Define Sample Data
  12. We are almost there. Select Modeling > ORM > Generate Code...
    Generate Code
  13. In Database Code Generation dialog, select generate Code and Database.
    Generate Code and Database
  14. Select Generate Sample Code so that we can test the implementation with the generated testing program.
    Generate Sample
  15. Press the Advanced Settings button.
    Advanced Setting
  16. In the Advanced Settings dialog, select ORM Implementation as Persistent API return type.
    ORM Implementatio as Persistent API return type
  17. Switch to Database tab and select Export to database. Select Yes (With Auto Generated PK) in Generate Sample Data. Press OK to proceed code generation.
    Export to database
  18. Now the Hibernate code are being generated, and we can start define the custom implementation in the getVolumeDiscountPrice method. i.e. we offer 10% off for purchase 5+ copies, and 20% off for purchase 10+ copies.
    Custom Implementation
  19. Done. Let's modify printout section in the list data sample to test our custom implementation.
    Modified Sample
  20. Run the modified list data sample and the custom implementation method was being called.
    Result


Turn every software project into a successful one.

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK