-->
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.  [ 2 posts ] 
Author Message
 Post subject: How do I persist a single class to 2 normalized tables? (Ho
PostPosted: Tue Sep 20, 2005 4:51 pm 
Newbie

Joined: Tue Sep 20, 2005 3:51 pm
Posts: 18
Location: Boston, MA
Hello All.

I have a normalized legacy table structure I’m trying to map to a legacy class structure. I have 2 tables, named “items” and “item_types” (a lookup table), in which I’ve included the SQL DDL. I want to map them to a class defined below. I’ve included the mapping I’ve used which works great for querying the data. However, I’d like to save the data as well. I couldn’t figure out how to make Hibernate aware of the sequence in the item_types look-up table joined to my main table. I’m working with a legacy database and legacy class system, so I cannot easily create classes for each table.

I’d like to instantiate a class, assign a value to the classes types property and have it persist to the item table with the correct foreign key. Ideally, if a new “item_types.type” is added, it would get the next sequence value, insert the new type into item_types, and insert the new item with the correct foreign key for the new item type.

I assume I need to use the <join> tag, but I couldn’t figure out how to make it aware of my sequence.

Thanks in advance
Steven

Here's my mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="Item" table="items">
        <id name="id" column="id">
      <generator class="sequence">
                   <param name="sequence">types_sequence</param>
           </generator>
        </id>
        <property name="prop1" column="prop1"/>
        <property name="prop2" column="prop2"/>
   <join table="item_types">
      <key column="type_id"/>
      <property name="type" column="type"/>
   </join>
    </class>
</hibernate-mapping>


Here's my tables:
Code:
create table items(
   id int primary key,
   prop1 varchar2(50),
   prop2 varchar2(50),
   ...
   item_type int references item_types(type_id)
);

create table item_types(
   type_id int primary key,
   type varchar(25)
);

Here's my class:
Code:
public class Item{
   int id;
   String prop1;
   String prop2;
   String type;
}


I'm using Hibernate 3 on Oracle9i.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 5:04 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
If you cannot easily create classes for each table and use "joined-subclass" tag, just let the db to manage the id by creating a sequence and an insert trigger for this table where that insert seq.nextval into the id field.

An example I usses that works with Hibernate or other insert method:

CREATE OR REPLACE TRIGGER TRG_BI_TABLENAME
BEFORE INSERT ON TableName
FOR EACH ROW
BEGIN
IF :NEW.tableNameId IS NULL OR :NEW.tableNameId = 0 THEN
SELECT SEQ_TABLENAME.NEXTVAL INTO :NEW.tableNameId FROM DUAL;
END IF;
END;
/


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