Fluent Nhibernate Composite Key Generator
13.12.2020
- Details
- Written by Nam Ha Minh
- Last Updated on 16 July 2019 Print Email
- Nhibernate Tutorial
- Fluent Nhibernate Mapping
- Fluent Nhibernate Example
- Fluent Nhibernate Composite Key Generator Free
Jul 19, 2010 NHibernate Mapping Generator. A simple utility to generate NHibernate mapping files and corresponding domain classes from existing DB tables. Features: Supports Oracle, SqlServer, PostgreSQL, MySQL, SQLite, Sybase, Ingres, CUBRID; Can generate hbm.xml, Fluent NHibernate and NH 3.3 Fluent style of mapping files. Jul 11, 2012 This example demonstrates how to work on mappings with associations for legacy database tables designed purely using multiple primary keys/composite keys. The article NHibernate and Composite Keys by Anne Epstein provides a thorough explanation on fundamental composite keys mapping and supporting lazy loading by declaring the composite keys as a class object.
Table of content:1. Creating Database and Tables
Execute the following MySQL script to create the database booksdb with two tables author and book: The database structure looks like this:2. Creating Eclipse Project
Create a Maven project in Eclipse with the following structure:The project consists of the following files:- Model classes: Author.java and Book.java
- Hibernate XML configuration file: hibernate.cfg.xml
- Test program: BooksManager.java
- Maven project: pom.xml
Nhibernate Tutorial
mysql-connector-java. Maven will resolve other related dependencies automatically.3. Coding Model Classes with Annotations
The followings are code of the two model classes: Author.java and Book.java.File netcodejavahibernateAuthor.java:File netcodejavahibernateBook.java: As you notice, we use the following JPA annotations:- @Entity: is required for every model class.
- @Table: maps the class with the corresponding database table. If omitted, Hibernate will use the class name.
- @Column: maps the field with the corresponding table column. If omitted, Hibernate will infer the column name and type based on signatures of the getter/setter.
- @Id and @GeneratedValue: are used in conjunction for a field that maps to the primary key. The values for this field are auto generated.
- @Temporal: must be used with a java.util.Date field to specify the actual SQL type of the column.
- @OneToOne and @JoinColumn: are used together to specify a one-to-one association and the join column.
4. Writing Hibernate Configuration File
Write XML configuration for database settings and mapping classes in the hibernate.cfg.xml file as follows:5. Writing a Test Program
Write code for the test program (BooksManager.java) as follows:Output of the program:Result in theFluent Nhibernate Mapping
Book table:Result in the Author table:Related Hibernate One-to-One Tutorials:Fluent Nhibernate Example
Other Hibernate Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook.Fluent Nhibernate Composite Key Generator Free
[Eclipse-Maven project] | 15 kB |