FFL2:FFL2 Main/Hibernate Reverse Engineering/Hibernate Reverse Engineering Example
In this example we are adding views for real league positions to the database and creating the associated classes and mappings.
When we run a Hibernate reverse engineer it will reverse in both tables and views. However our life will be made easier by creating dummy tables first, then reverse engineering, then replacing them with views of the same name. The reason for this is that we can then define the primary keys and foreign key associations, without these the reverse engineer wouldn't give us exactly what we wanted and we'd be left with manual editing to do. In addition the types of the number would be incorrect (we get things like BigDecimal(45, 0) ).
Define the table
We will add a table as follows
create table RealLeagueStanding ( idRealClub int(11) unsigned not null, seasonStartYear int(11) unsigned not null, idRealDivision int(11) unsigned not null, played int(3) unsigned not null, won int(3) unsigned not null, drawn int(3) unsigned not null, lost int(3) unsigned not null, goalsFor int(3) unsigned not null, goalsAgainst int(3) unsigned not null, homeWon int(3) unsigned not null, homeDrawn int(3) unsigned not null, homeLost int(3) unsigned not null, homeGoalsFor int(3) unsigned not null, homeGoalsAgainst int(3) unsigned not null, awayWon int(3) unsigned not null, awayDrawn int(3) unsigned not null, awayLost int(3) unsigned not null, awayGoalsFor int(3) unsigned not null, awayGoalsAgainst int(3) unsigned not null, goalDifference int(3) not null, points int(3) not null, primary key (idRealClub, seasonStartYear), CONSTRAINT `temp1rls` FOREIGN KEY (`idRealClub`) REFERENCES `RealClub` (`idRealClub`), CONSTRAINT `temp2rls` FOREIGN KEY (`seasonStartYear`) REFERENCES `FantacySeason` (`seasonStartYear`), CONSTRAINT `temp3rls` FOREIGN KEY (`idRealDivision`) REFERENCES `RealDivision` (`idRealDivision`) ) ENGINE=InnoDB;
Some points to note:
- By specifying the columns as not null we will get int primitives in the generated Java. If we did not specify 'not null' we would get Integer objects as variables of this type can be null.
- The constraints will give cause associations to the relevant classes to be generated.
Run the Reverse Engineer
Run it using 'Run | Hibernate Code Generation... | Reverse Engineer FFL Model from DB'. It will take a minute or two and then you should find all the generated code in the fantacycore/hibernate_gen_src folder.
Merge in the Changes
In the Eclipse 'Java' perspective open up the 'Navigator' view (use 'Window | Show View' if required). We will use the navigator view as it just deals with files and doesn't try to do anything clever.
Copy the following files over directly from the hibernate_gen_src/uk/org/fantacy/model/ directory to the src/uk/org/fantacy/model/ directory as they are new
- RealLeagueStanding.java
- RealLeagueStandingId.java
- RealLeagueStanding.hbm.xml