This plugin generates pojos and mapping files for hibernate.
It cares about associations by parsing foreign keys.
Suppose we have two tables in a database :
Here is the corresponding MySQL DDL :
CREATE TABLE city ( id_city int(11) NOT NULL auto_increment, name varchar(50) NOT NULL, PRIMARY KEY (id_city) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE person ( id_person int(11) NOT NULL auto_increment, name varchar(50) NOT NULL, first_name varchar(50) NOT NULL, age int(11) NOT NULL, id_city int(11) NOT NULL, PRIMARY KEY (id_person), KEY fk_person_city (id_city), CONSTRAINT fk_person_city FOREIGN KEY (id_city) REFERENCES city (id_city) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
A person lives in a city, and for each city there are many persons living in it. So we have a many-to-one relationship.
Hibernate plugin generates two classes in this case, City and Person :
package com.salto.hibernate; import java.util.List; import java.io.Serializable; import java.sql.Timestamp; /** * Pojo mapping table city * * * Generated at Tue Feb 06 10:10:55 CET 2007 * @author Salto-db Eclipse v1.0.14 * */ public class City implements Serializable { /** * Attribute idCity. */ private Integer idCity; /** * Attribute name. */ private String name; /** * List of Person */ private List<Person> persons = null; /** * @return idCity */ public Integer getIdCity() { return idCity; } /** * @param idCity new value for idCity */ public void setIdCity(Integer idCity) { this.idCity = idCity; } /** * @return name */ public String getName() { return name; } /** * @param name new value for name */ public void setName(String name) { this.name = name; } /** * Get the list of Person */ public List<Person> getPersons() { return this.persons; } /** * Set the list of Person */ public void setPersons(List<Person> persons) { this.persons = persons; } }
package com.salto.hibernate; import java.util.List; import java.io.Serializable; import java.sql.Timestamp; /** * Pojo mapping table person * * * Generated at Tue Feb 06 10:10:56 CET 2007 * @author Salto-db Eclipse v1.0.14 * */ public class Person implements Serializable { /** * Attribute idPerson. */ private Integer idPerson; /** * Attribute name. */ private String name; /** * Attribute firstName. */ private String firstName; /** * Attribute age. */ private Integer age; /** * Attribute city */ private City city; /** * @return idPerson */ public Integer getIdPerson() { return idPerson; } /** * @param idPerson new value for idPerson */ public void setIdPerson(Integer idPerson) { this.idPerson = idPerson; } /** * @return name */ public String getName() { return name; } /** * @param name new value for name */ public void setName(String name) { this.name = name; } /** * @return firstName */ public String getFirstName() { return firstName; } /** * @param firstName new value for firstName */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * @return age */ public Integer getAge() { return age; } /** * @param age new value for age */ public void setAge(Integer age) { this.age = age; } /** * get city */ public City getCity() { return this.city; } /** * set city */ public void setCity(City city) { this.city = city; } }
Hibernate plugin generates also the mapping file and the hibernate.cfg.xml file :
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- This file has been generated by Salto-db Eclipse v1.0.14 at Tue Feb 06 10:10:56 CET 2007 --> <hibernate-mapping> <class name="com.salto.hibernate.City" table="city"> <id name="idCity"> <generator class="native"/> </id> <property name="name" column="name"/> <bag name="persons" inverse="true"> <key column="id_city"/> <one-to-many class="com.salto.hibernate.Person"/> </bag> </class> </hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- This file has been generated by Salto-db Eclipse v1.0.14 at Tue Feb 06 10:10:56 CET 2007 --> <hibernate-mapping> <class name="com.salto.hibernate.Person" table="person"> <id name="idPerson"> <generator class="native"/> </id> <property name="name" column="name"/> <property name="firstName" column="first_name"/> <property name="age" column="age"/> <many-to-one name="city" column="id_city"/> </class> </hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- This file has been generated by Salto-db Eclipse v1.0.14 at Tue Feb 06 10:10:56 CET 2007 --> <hibernate-configuration> <session-factory> <!-- You need to complete the configuration here. This is just a sample, you should use a connection pool--> <property name="connection.url">jdbc:mysql://localhost/sdb</property> <property name="connection.username">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- Your database is MySQL--> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.password">XXXXXXXXX</property> <property name="show_sql">true</property> <!-- Remove this property if you use JTA --> <property name="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property> <mapping resource="City.hbm.xml" /> <mapping resource="Person.hbm.xml" /> </session-factory> </hibernate-configuration>
Once these files have been generated, you can directly use them in your project to load and to persist your data. Of course you still need to be familiar with Hibernate to do so...