-->
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.  [ 5 posts ] 
Author Message
 Post subject: Newbie: Help With Mappings
PostPosted: Thu Aug 05, 2004 12:21 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 4:58 pm
Posts: 37
I am using Hibernate 2.1.4

I have the following psuedo tables

[Employee]
empid (pk)
firstname
lastname
department
email

--------------------

[User]
userid (pk/fk to Employee.empid)
password
role
status

--------------------

[System]
systemid
systemname

---------------------

[Issue]
issueid (pk)
issuenumber
datecreated
datemodified
problemtext
createdby (fk User.userid)
acceptedby (fk User.userid)
closedby (fk User.userid)
assignedto (fk User.userid)
employeeid ((fk Employee.empid)
systemId (fk System.systemid)

----------------------

And I am having a really hard time figuring out some mappings. Here is what I have so far

[Issue.hbm.xml]
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

    <class name="com.intrustbank.anykey.beans.Issue" table="issue_table">

        <id name="issueId" type="int" column="isueid" unsaved-value="0">
            <generator class="identity"/>
        </id>

        <property name="dateCreated" column="datecreated"  type="java.util.Date" />
        <property name="dateModified" column="datemodified"  type="java.util.Date"/>
        <property name="issueNumber" column="issuenumber" type="java.lang.String"/>

         <many-to-one
            name="systemId"
            column="systemid"
            class="com.intrustbank.anykey.beans.System"
            not-null="false" />
       

        <property name="additionalInfo" column="problemtext" type="java.lang.String"/>

        <many-to-one
            name="createdBy"
            column="createdby"
            class="com.intrustbank.anykey.beans.User"
            not-null="false" />
       
    </class>

</hibernate-mapping>


[Employee.java]
Code:
package com.intrustbank.anykey.beans;

public class Employee
{
    private int empId;
    private String firstName;
    private String lastName;
    private String department;
    private String email;

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}


[User.java]
Code:
package com.intrustbank.anykey.beans;

public class User extends Employee
{
   private String role;
   private String email;
   private int status;

   public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }


}


Can someone tell me if I am even on the right track here? Sorry for the length of the post. :)

_________________
Gregg Bolinger
Javaranch Sheriff
Weblog


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 2:19 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 2:15 pm
Posts: 35
you are on the right track.. for the most part?


it seems like everyone of your Employees is a User also? then why another table?



public class User extends Employee

this should be mapped something like:

<hibernate-mapping>

<class name="com.intrustbank.anykey.beans.Employee"
...... rest of the stuff
......

<subclass name="User" discriminator-value="U">
<property name="password"/>
<property name="role"/>
<property name="status"/>
</subclass>


</class>

</hibernate-mapping>


and your table should look like:
[Employee]
empid (pk)
firstname
lastname
department
email
password
role
status

_________________
--------------------------------------------------
To code or not to code... Is that a question?
--------------------------------------------------
Pritpal Dhaliwal


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 2:42 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 4:58 pm
Posts: 37
Thanks. The reason I had a User Table and an Employee table is because there are 1500 Employees. Only 12 are users of this system. So I thought it better to sepearte the 2 tables. Is that bad design?

_________________
Gregg Bolinger
Javaranch Sheriff
Weblog


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 2:43 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 4:58 pm
Posts: 37
Also, if I needed to get a list of all Users, it would be easier to pull that from a User table than to try and filter the Employees or adding an extra column called user (yes/no).

_________________
Gregg Bolinger
Javaranch Sheriff
Weblog


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 4:11 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
IMHO having a separate table for employee and user isn't a good idea, unless you really know that you have to do it like that. This is especially true if you have to acess the data without Hibernate's help as well. Generally speaking: Having all classes of hirarchy in one table is the simplest solution.

Hibernate in Action has a very nice description about this in chapter "3.6 Mapping class inheritance". You can get an online verison at http://www.manning.com.

HTH
Ernst


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.