I am using Hibernate Tools to generate POJOs and Mapping files. Everything works well but entity relationships are not established properly either in POJOs or Mapping files.
Here is the hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="session1">
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/travel</property>
<property name="hibernate.connection.username">travel</property>
<property name="hibernate.connection.password">travel</property>
</session-factory>
</hibernate-configuration>
hibernate.reveng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<table-filter exclude="false" match-catalog=".*" match-name="FLIGHT" match-schema=".*"/>
<table-filter exclude="false" match-catalog=".*" match-name="PERSON" match-schema=".*"/>
<table-filter exclude="false" match-catalog=".*" match-name="TRIP" match-schema=".*"/>
<table-filter exclude="false" match-catalog=".*" match-name="TRIPTYPE" match-schema=".*"/>
</hibernate-reverse-engineering>
Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 3, 2008 2:34:25 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="travel.Person" table="PERSON" schema="TRAVEL">
<id name="personid" type="int">
<column name="PERSONID" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="NAME" length="50" />
</property>
<property name="jobtitle" type="string">
<column name="JOBTITLE" length="50" />
</property>
<property name="frequentflyer" type="java.lang.Short">
<column name="FREQUENTFLYER" />
</property>
<property name="lastupdated" type="timestamp">
<column name="LASTUPDATED" length="26" />
</property>
<set cascade="all-delete-orphan" inverse="true" lazy="true" name="trips" table="TRIP"> <key column="PERSONID"/> <one-to-many class="travel.Trip"/> </set> </class>
</hibernate-mapping>
I manually added <set>. But I expected the <set> to be generated by default using the hibernate Tools.
Person.java
package travel;
// Generated Jul 3, 2008 2:34:20 PM by Hibernate Tools 3.2.1.GA
import java.util.Date;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Person generated by hbm2java
*/
@Entity
@Table(name="PERSON"
,schema="TRAVEL"
)
public class Person implements java.io.Serializable {
private int personid;
private String name;
private String jobtitle;
private Short frequentflyer;
private Date lastupdated;
private Set<Trip> trips;
@OneToMany(mappedBy="personid") public Set<Trip> getTrips() { return trips; }
public void setTrips(Set<Trip> trips) { this.trips = trips; }
public Person() {
}
public Person(int personid) {
this.personid = personid;
}
public Person(int personid, String name, String jobtitle, Short frequentflyer, Date lastupdated) {
this.personid = personid;
this.name = name;
this.jobtitle = jobtitle;
this.frequentflyer = frequentflyer;
this.lastupdated = lastupdated;
}
@Id
@Column(name="PERSONID", unique=true, nullable=false)
public int getPersonid() {
return this.personid;
}
public void setPersonid(int personid) {
this.personid = personid;
}
@Column(name="NAME", length=50)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="JOBTITLE", length=50)
public String getJobtitle() {
return this.jobtitle;
}
public void setJobtitle(String jobtitle) {
this.jobtitle = jobtitle;
}
@Column(name="FREQUENTFLYER")
public Short getFrequentflyer() {
return this.frequentflyer;
}
public void setFrequentflyer(Short frequentflyer) {
this.frequentflyer = frequentflyer;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="LASTUPDATED", length=26)
public Date getLastupdated() {
return this.lastupdated;
}
public void setLastupdated(Date lastupdated) {
this.lastupdated = lastupdated;
}
}
In the same way I had to manually add the code (in bold) in Person.java
Here is how I am generating the POJOs and Mappings:
// Configuring the reverse engineering strategy
try {
cfg = new JDBCMetaDataConfiguration();
OverrideRepository or = new OverrideRepository();
Configuration c = cfg.configure(confFile);
or.addFile(FileUtil.toFile(revengFile));
DefaultReverseEngineeringStrategy strategy = new DefaultReverseEngineeringStrategy();
settings = new ReverseEngineeringSettings(strategy);
settings.setDefaultPackageName(helper.getPackageName());
strategy.setSettings(settings);
cfg.setReverseEngineeringStrategy(or.getReverseEngineeringStrategy(strategy));
cfg.readFromJDBC();
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
// Generating POJOs
try {
if (helper.getDomainGen()) {
POJOExporter exporter = new POJOExporter(cfg, outputDir);
exporter.getProperties().setProperty("jdk", new Boolean(helper.getJavaSyntax()).toString());
exporter.getProperties().setProperty("ejb3", new Boolean(helper.getEjbAnnotation()).toString());
exporter.start();
}
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
// Generating Mappings
try {
if (helper.getHbmGen()) {
HibernateMappingExporter exporter = new HibernateMappingExporter(cfg, outputDir);
exporter.start();
}
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
Wondering if I have to set anything in order to generate the relationships.
Note that the database has relationships defined.
I appreciate your help
Thanks - G
|