-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: confused about mapping a legacy database
PostPosted: Wed Apr 08, 2009 12:38 am 
Newbie

Joined: Wed Feb 23, 2005 9:57 pm
Posts: 16
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>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 7:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
your table has no primary key - define one.

Either by making your db schema have it or mark it in reveng.xml.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 1:23 pm 
Newbie

Joined: Wed Feb 23, 2005 9:57 pm
Posts: 16
Ok, thanks. I was fuzzy on the concept of entities and non-entities.

I'll live with the id generated by Hibernate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.