How to Generate Code and Database?
- Select Tools > Hibernate > Generate Code... from the toolbar.
- Fill in the Output Path and select code generation options.
Database code generation Option Description Framework Determines how the mapping between class model and ER model is to be recorded.
Hibernate XML - Hibernate mapping file (.hbm.xml) will be generated. It contains the mapping between class model and ER model in XML format.
JPA - Not to generate the Hibernate mapping file but to store the mapping information directly in the generated Java source file as annotations.
Error Handling For Error Handling, select the way to handle errors. The possible errors include PersistentException, GenericJDBCException, and SQLException.
Return false/null - It returns false/null in the method to terminate its execution.
Throw PersistentException - It throws a PersistentException which will be handled by the caller. A try/catch block has to be implemented to handle the exception.
Throw RuntimeException - It throws a RuntimeException which will be handled by the caller. A try/catch block has not been implemented to handle the exception. The exception will be caught at runtime.
Throw User Defined Exception - It throws an exception that is defined by user. Select this to input the fully qualified name of the Exception class.
Exception Handling For Exception Handling, select how to handle the exception.
Do not Show - It hides the error message.
Print to Error Stream - It prints the error message to the Error Stream.
Print to log4j - It prints the error message to the log4j library.
Default Lazy Collection Initialization Lazy collection initialization avoids the associated objects from being loaded when the main object is loaded. With lazy collection initialization, all associated object (1 to many) will not be loaded until you access it (e.g. getFlight(0)). Enabling this option can usually reduce more than 80% of the database loading.
Lazy - You must update both ends of a bi-directional association manually to maintain the consistency of the association. Besides, casting of object(s) to its corresponding persistence class is required when retrieving object(s) from the collection.
Extra - When you update one 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.
Non-lazy - Load the associated objects when the main object is loaded.
Default Lazy Association Initialization Each association can set lazy. This setting is for those associations that has lazy set as Unspecified.
Proxy fetching - A single-valued association is fetched when a method other than the identifier getter is invoked upon the associated object.
"No-proxy" fetching - A single-valued association is fetched when the instance variable is accessed. Compared to proxy fetching, this approach is less lazy; the association is fetched even when only the identifier is accessed. It is also more transparent, since no proxy is visible to the application. This approach requires buildtime bytecode instrumentation and is rarely necessary.
False - Not to set lazy.
Association Handling Smart association handling, updating either side of a bi-directional association will automatically trigger an update on the other side. For example:
many.setOne(one);
or
one.many.add(many);
It also provides static type checking by using strong type collection.
With Standard association handling, you will need to update both sides of a bi-directional association to maintain consistency. For example:
many.setOne(one);
one.getMany().add(many);
With Generics association handling, you will need to update both sides of a bi-directional association to maintain consistency, which is the same as the Standard association handling as described above, except that for Generics association handling, generics (e.g. Set<Account>) has been used for type specialization.
Persistent API For Persistent API, select the type of persistent code to be generated, either Static Methods, Factory Class, DAO or POJO. The decision to which API to select depends on the practice of coding.
Static Methods - Client can create, retrieve and persist with the PersistentObject directly.
Factory Class - FactoryObject class will be generated for client to create and retrieve the PersistentObject. Client can directly persist with the PersistentObject.
DAO - The PersistentObjectDAO class helps client to create, retrieve and persists to PersistentObject.
DAO (with Interface) - A variation of DAO. In DAO (with Interface), the DAO class become an interface, and the implementation is moved to the DAOImpl class. Instance methods are used instead of Static methods. With these changes, you can define your own DAO objects and swap between DAO implementations easily.
POJO - The PersistentManager helps client to retrieve and persist with PersistentObject.
Mapping only - Just to generate the mapping without generating any code file.
Generate Criteria Check the option to generate the criteria class for each ORM Persistable class. The criteria class is used for querying the database in object-oriented way. Serializable Generate implement Serializable in Java, [Serializable] in C#. Cache Options Configure Second Level Cache to improve performance. Selected Optional Jar Select the libraries and JDBC driver to be included in the generation of the orm.jar file (Persistent Library). Advanced Settings Click to edit some of the advanced settings:
Default Order Collection Type - Select the type of ordered collection to be used in handling multiple cardinality relationship, either List, Array or Map.
Default Un-Order Collection Type - Select the type of un-ordered collection to be used in handling the multiple cardinality relationship, either Set or Bag.
Override toString Method - Select the way that you want to override the toString method of the object. There are three options provided to override the toString method as follows:
- All Properties - the toString method returns a string with the pattern
- ID Only - the toString method returns the value of the primary key of the object as string.
"Entity[<column1_name>=<column1_value><column2_name>=(column2_value>...]" - Business Key - You can specify business key per class, inside the Business Key tab of class specification. Business keys comprise of attributes. The toString method will return a string of business keys.
- No - the toString method will not be overridden
Flush Mode - Select the Flush Mode to be used in flushing strategy. User can select Auto, Commit, Always and Never.
- Auto - The Session is sometimes flushed before executing query.
- Commit - The Session is flushed when committing Transaction.
- Always - The Session is flushed before every query.
- Never - The Session is never flushed unless the flush method is called.
Mapping File Column Order - <to-be-entered>
Getter/Setter Visibility - Set the visibility of getter and setter methods for attributes in persistable classes. By selecting Public, public getters and setters will be generated. By selecting Follow attribute, the visibility will follow the visibility of attributes.
Generate Mapping - Overwrite or update XML mapping files
Mapping type for date - Select hibernate type for mapping date type: Date, Time, Timestamp
Composite ID with Association - Generate <key-many-to-one> or <key-property> for composite ID, <key-many-to-one> has some bugs and <key-property> is recommended.
Generate property constant - Generate constant for each properties, best for building customized HQL or Criteria.
Generate lower case package - It is preferred to use lower case for package name in Java. Check this to enforce the use of lower case for package name no matter the package element in project has lower or upper case as name.
Separate subclass mapping file - Generate subclass's XML mapping file in separate file, instead of generating in parent class mapping file.
Attribute Prefix - Prefix of names of attributes in persistable classes.
Persistent API return type - DAO method return <<ORM Implementation>> subclass.
Public ID setter - Generate ID setter as public.
Public Version setter - Generate Version property setter as public.
Generate custom annotation - Generate Java annotations defined in model specification.
Generate non-persistable association - Generate non-persistable association in persistable class.
Generate @SuppressWarning("all") annotation - Generate @SuppressWarning("all") annotation to reduce compiler warnings.
Generate sessionless methods - Generate DAO methods without session parameter.
Property access - Hibernate access attribute by getter/setter methods.
Generate validator constraints - Generate hibernate validator annotation for validating String length.
Encoding - Encoding for generating source files.
Persistent Manager Package - Customize the package for PersistentManager or using default.
Persistent Collection - Customize persistent collection's implementation class for each collection type.
Non Persistent Collection - Customize non-persistent collection implementation class.
Samples Sample files, including Java application sample, Servlet sample and Java Server Page (JSP) sample are available for guiding you through the usage of the Java persistence class. You can check the option(s) to generate the type of sample files you need. Scripts Check the option(s) to generate Ant File/Batch/Shell scripts, for the direct execution of generated code. Generate Filter and Web Application Descriptor (web.xml) Determines whether to generate the file web.xml essential in Web application development. Available options in ORM code generation - Click OK button to start code generation.
Related Resources
The following resources may help you to learn more about the topic discussed in this page.
Chapter 6. Generating Object-Relational Mapping Code | Table of Contents | 2. Lazy collection setting |