-->
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: How to model defined with undefined entities
PostPosted: Wed Sep 22, 2004 5:30 pm 
Newbie

Joined: Mon Aug 30, 2004 2:48 pm
Posts: 11
Hibernate version:3.0

I've posted something similar to this elsewhere, but I want to repeat it to refine it. I've spent more than a week trying to model my domain but I can't seem to make it work with Hibernate.

Let me give you an overview of my schema. I have three tables of interest:

Documents
doc_id
fc_id
name
...

Filecabinets
fc_id
title
tbl_name
num_flds
...

FileCabinetFields
fc_id
fld_id
col_name
col_type
col_size
....


Then their is a 4th table that is not defined in my schema. It is created by the application and its field names and types are stored in the FileCabinets table. Basically it is a way to provide further properties for each row in the Documents table. The only thing that every one of these tables has is:
doc_id

These external tables act as extra columns for each Document, but they are different for each filecabinet because each filecabinet needs different properties.

As you can probably deduce they have the following relationships:

A FileCabinet has many documents
A FileCabinet has many fields
A Field is associated with a column in an external table



Now, I know that Hibernate 3 isn't released for production but I thought I would try and see if it's mapping capabilities would be able to model my domain. I envision the mapping for my domain to look something like this





Code:
<class name="document">
  <id name="id" column="doc_id" type="int" >
   <generator class="hilo"/>
  </id>
  <property name="title" />

   ....<!-- The other properties that are defined in the concrete document table -->

   ...


   <Unmapped Properties />

   ...   

</class>




<Unmapped Properties />
This is where I need some way to map to the 4th table. Basically it is a
dynamic join. I would would do a join to whatever table is specified
in the filecabinet that owns the document.

I realize this might not be built in to Hibernate, but if someone could PLEASE point me in the direction as far as where I want to extend a class
somewhere so I can try and get this functionality.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 4:54 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
May be this helps:
For your 4th table, instead of creating dynamically you could have something lik this:

AddinalPropertiesTable
apt_id
foreign_doc_id
type
name
value

You could thinkt about creating a CompositeUserType for type, name and value to have a comfortoable way to deal with it in the business logic.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 9:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Also, take a look at the <any/> mapping element.


Top
 Profile  
 
 Post subject: Need help designing <any> mapping
PostPosted: Thu Sep 23, 2004 10:28 am 
Newbie

Joined: Mon Aug 30, 2004 2:48 pm
Posts: 11
Thank you both for responding.

For one, I can't change the schema as it is a legacy application, and even if I could, I don't know that the additional_properties table would be the best way to go since your value column would hold values of multiple types, and you'd have to make it as large as your largest value (Wouldn't be very good for indexing millions of rows).

I've seen the <any> mapping in the reference documentation and looked at the FooBar.hbm.xml mapping document that contains a <any> mapping, but I still can't for the life of me figure out how to use it and how it actually maps to a real document. Gavin gave this example in another post of how the <any> type works:

Code:


<class name="Foo" ...>
   <id name="id" type="long">
      ....
   </id>
   ....
</class>

<class name="Bar" ...>
   <id name="id" type="long">
      ....
   </id>
    ...
</class>

<class name="X">
   ....
   <any id-type="long" meta-type="class">
      <column name="referenced_class"/>
      <column name="foo_or_bar_id"/>
   </any>

</class>



First of all, could you please show me what the tables that this mapping supports would look like, and what the resulting POJO would look like? If someone else has any examples that use an <any> tag for polymorphic associations to undefined tables, could I see an example of how you are using it. Thanks./


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 10:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you want to see what the schema looks like, run hbm2ddl!


Top
 Profile  
 
 Post subject: Still don't understand
PostPosted: Thu Sep 23, 2004 2:17 pm 
Newbie

Joined: Mon Aug 30, 2004 2:48 pm
Posts: 11
Gavin,

Thanks for the advice. I generated the DDL and the POJOS for the following HBM XML file:

Code:
<class name="Foo" >
  <id name="Foo_id" type="long">
   <generator class="hilo" /> 
  </id>
</class>

<class name="Bar" >
  <id name="Bar_id" type="long">
    <generator class="hilo" /> 
  </id>
</class>

<class name="X">
  <id name="X_id" type="long">
    <generator class="hilo" /> 
  </id>

  <any name="any_type" id-type="long" meta-type="class">
    <column name="referenced_class"/>
    <column name="foo_or_bar_id"/>
  </any>
</class>


That generated the following POJOs:

Code:
public class X implements Serializable {

  private Long X_id;
  private Object any_type;
   
  public X(Object any_type) {...}
  public X() {}
  public Long getX_id() {...}
  public void setX_id(Long X_id) {...}
  public Object getAny_type() {...}
  public void setAny_type(Object any_type) {...}
}

....

public class Bar implements Serializable {

  private Long Bar_id;
  public Bar() {}
  public Long getBar_id() {...}
  public void setBar_id(Long Bar_id) {...}
}
...



And the following SQL DDL:

Code:

Table X:
  X_id             | bigint(20)   
  referenced_class | varchar(255)
  foo_or_bar_id    | bigint(20) 

Table Bar:
  Bar_id | bigint(20)

...




Ok, so now that I have all this down, what does this do? How am I to apply this to my problem?

In the reference manual, you have the following example:

Code:

<any name="anyEntity" id-type="long" meta-type="eg.custom.Class2TablenameType">
    <column name="table_name"/>
    <column name="id"/>
</any>



Now one of the columns is table_name which makes me think I can somehow use <any> to solve my problem. But I really don't understand how this is implemented. Could someone please connect all these dots?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 7:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You need to add the meta-values to define the "rules" that map a given value in the meta-column to a class:
Code:
<any name="anyEntity" id-type="long" meta-type="string">
    <meta-value value="TABLE_X" class="ClassX"/>
    <meta-value value="TABLE_Y" class="ClassY"/>

    <column name="table_name"/>
    <column name="id"/>
    ....
</any>


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.