-->
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.  [ 7 posts ] 
Author Message
 Post subject: Lookup Table - Best Practices help needed
PostPosted: Mon Sep 25, 2006 7:31 pm 
Newbie

Joined: Mon Sep 25, 2006 5:02 pm
Posts: 3
I have read the reference manual and looked at lots of the code examples included in the Hibernate 3.1 download, however I have been unable to locate a lookup table example.

I have a Manufacturer's table with two fields, ID and Name. This table is the lookup table. It rarely changes. I have another table called Products. It has several fields, one of which is ManufacturerID (foreign key) to the Manufacturer's table. I want to link the two tables via this key.

The Java classes for these two classes are easy to create, but I'm not sure of the best way to map the classes in the hbm.xml files. All the examples I have found so far always create data for both tables in the examples. I essentially want the Products table to see the Manufacturer's table as a read-only table.

Can someone point me to an example that I might have overlooked. I'm sure it exists and I have just overlooked it so far.

Thanks
Phil Willemann


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 8:33 pm 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
use a,

Code:
<many-to-one update="false" insert="false" />


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 10:16 pm 
Newbie

Joined: Mon Sep 25, 2006 5:02 pm
Posts: 3
I tried that to no avail. Infact that is what I currently have.

Here is the code in my product.hbm.xml

<many-to-one name="ManufacturerID"
column="ID"
class="Manufacturer"
update="false"
insert="false"
/>

My product.java class has:

private Integer ManufacturerID;

with a standard getter and setter pair.

My manufacturer.hbm.xml is

<id name="ManufacturerID" column="ID" type="integer">
<generator class="native"/>
</id>
<property name="Name" column="Name" type="string"/

With this setup I always get this error:

ERROR BasicPropertyAccessor:167 - IllegalArgumentException in class:

I must be missing something else


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 10:39 pm 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
Shouldn't your product class have,
Code:
private Manufacturer manufacturer;

instead of,
Code:
private Integer ManufacturerID;

and the product mapping file be,
Code:
<many-to-one name="Manufacturer"
column="foreignKeyColumn" ... />



If you just want the manufacturer id to be retrieved then do

<property name="manufacturerId" column="foreignKeyColumn" />

_________________
George F.
Don't forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 10:34 am 
Newbie

Joined: Mon Sep 25, 2006 5:02 pm
Posts: 3
I think I am catching on - instead of my class keeping track of the Id for the Manufacturer - you are saying I should embed the Manufacturer object in the Product object.

That makes sense -- now here is my question. Assuming my program has a combo box of Manufacturers and a user selects one when adding a product, I am going to need some sort of method that will find the appropriate Manufacturer object in my table so I can save my Product.


    User picks Manufacturer "Walmart"
    Program queries the Manufacturer table for Walmart and returns the manufacturer object for Walmart
    Program saves Product (with "Walmart" info")



Does this make sense?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 5:23 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 10:20 am
Posts: 32
Location: Brazil
That's right. Assuming you're developing a webapp, you should put the manufacturer's id in the select's value:
Code:
<select name="manufacturer">
  <option value="1">Walmart</option>
  <option value="2">ACME</option>
</select>

And then
Code:
String manufacturerId = request.getParameter(manufacturer);
Manufacturer manufacturer = session.load(manufacturerId, Manufacturer.class);
Product product = new Product(...);
product.setManufacturer(manufacturer);
session.save(product);

Or something similar.

Hope this helps,
Daniel Serodio


Top
 Profile  
 
 Post subject: Re: Lookup Table - Best Practices help needed
PostPosted: Tue Sep 26, 2006 5:34 pm 
Newbie

Joined: Thu Oct 09, 2003 4:06 pm
Posts: 16
philwillemann wrote:
I have read the reference manual and looked at lots of the code examples included in the Hibernate 3.1 download, however I have been unable to locate a lookup table example.

I have a Manufacturer's table with two fields, ID and Name. This table is the lookup table. It rarely changes. I have another table called Products. It has several fields, one of which is ManufacturerID (foreign key) to the Manufacturer's table. I want to link the two tables via this key.

The Java classes for these two classes are easy to create, but I'm not sure of the best way to map the classes in the hbm.xml files. All the examples I have found so far always create data for both tables in the examples. I essentially want the Products table to see the Manufacturer's table as a read-only table.

Can someone point me to an example that I might have overlooked. I'm sure it exists and I have just overlooked it so far.

Thanks
Phil Willemann


If these manufacturers are really static and won't change, have you looked at implementing a UserType? I use a UserType which is mapped to an integer in the database, but in my app it's a class using the old Enum pattern with static instances. Each subclass knows what table it represents in the database and, at startup, I can verify that the static instances are in sync with the values in the database. Do a search for enum and usertype and you'll find lots of discussions of implementing enumerated types.


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