I don't understand how to use the generated java code from the hibernate tools. I have 3 tables, USERS, HOSTS, and HOSTS_IN_USE. The USERS table has a primary key ID column, varchar NAME, etc. Same with the HOSTS table. The HOSTS_IN_USE table has 2 columns, USER_ID and HOST_ID; when a host is in use the IDs for it and the user using it are added to the HOSTS_IN_USE table. Here's the ddl for the HOSTS_IN_USE table using postgres:
Code:
CREATE TABLE waitlist2.hosts_inuse (
host_id integer NOT NULL,
user_id integer NOT NULL,
"time" timestamp without time zone NOT NULL,
CONSTRAINT hosts_inuse_host_id_fk FOREIGN KEY (host_id)
REFERENCES waitlist2.hosts (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT hosts_inuse_user_id_fk FOREIGN KEY (user_id)
REFERENCES waitlist2.users (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
The generated java for the Host class (from the HOSTS table) has in it
Code:
private Set<HostsInuse> hostsInuses = new HashSet<HostsInuse>(0);
with the usual getters and setters.
And then I have these other classes which I don't understand how I should use them:
Code:
/**
* HostsInuse generated by hbm2java
*/
@Entity
@Table(name="hosts_inuse"
,schema="waitlist2"
)
public class HostsInuse implements java.io.Serializable {
private HostsInuseId id;
private Host host;
private User user;
Code:
/**
* HostsInuseId generated by hbm2java
*/
@Embeddable
public class HostsInuseId implements java.io.Serializable {
private int hostId;
private int userId;
private Date time;
When I retrieve a host that's in the HOSTS_IN_USE table, its hostsInuses field (the one of type of Set<HostsInuse>) has an entry in it; if it's not in the HOSTS_IN_USE table the Set is empty.
What I'm confused about is how to write my code that checks to see if a Host is in use; should I just use host.hostsInuses.isEmpty() ?
I'm starting to feel that I need to write a class that presents a nicer interface than this Host class generated by the hibernate tools and convert each Host before I use it, in my dao. That seems klunky though.
I mapped this with hbm2java by having it inspect the database; I'm using the maven plugin:
Code:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>jdbcconfiguration</implementation>
</component>
<component>
<name>hbm2doc</name>
<implementation>jdbcconfiguration</implementation>
</component>
<component>
<name>hbm2hbmxml</name>
<implementation>jdbcconfiguration</implementation>
</component>
<component>
<name>hbm2ddl</name>
<implementation>jdbcconfiguration</implementation>
</component>
</components>
<componentProperties>
<configurationfile>src/main/hbm/hibernate.cfg.xml</configurationfile>
<revengfile>src/main/hbm/hibernate.reveng_${db.flavor}.xml</revengfile>
<format>true</format>
<!-- hbm2java -->
<jdk5>true</jdk5>
<ejb3>true</ejb3>
</componentProperties>
</configuration>
</plugin>
and I run maven with
Code:
mvn -Ptest1-postgres clean hibernate3:hbm2java
For the HOSTS and USERS table the reveng file has
Code:
<table schema="waitlist2" name="hosts" class="Host">
<primary-key>
<generator
class="native"
/>
<key-column
name="id"
/>
</primary-key>
</table>
<table schema="waitlist2" name="users" class="User">
<primary-key>
<generator
class="native"
/>
<key-column
name="id"
/>
</primary-key>
</table>