-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: mapping problems...please help!!!!!!
PostPosted: Mon Oct 24, 2005 2:46 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Hi,

I was trying to write the mapping files for one of my classes. But I do not know how I can map these:

1) I have a field in my class of type File, I want the corresponding table of the class to have just the absolute path of this file stored as VARCHAR. Since FILE is a java class and it doesnt i do not know how I shud map it or have a seperate table for it?? This inbuilt file class has a getter for the path but not a setter.I cud just have a seperate String that stores the path and just map that.But they do not want an extra field storing the path. How else can I do it?

2) I have an
EnumMap<someclass, inetger> a = new EnumMap<someclass, inetger>; I do not know how I shud map this??

Thanx,
Shefali


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 3:11 pm 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
Could you explain how you are going to use this in your class? Maybe a small example of what you are tring to do.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 3:31 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
One method would be to implement your own UserType. A user type is a class that you name in a mapping file which converts between the data as represented in the database and how it represented in the data model. I've done several were I have a custom class in my data model but I map it into the database as a string.


Top
 Profile  
 
 Post subject: Re: mapping problems...please help!!!!!!
PostPosted: Mon Oct 24, 2005 3:33 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
shef wrote:
Hi,

I was trying to write the mapping files for one of my classes. But I do not know how I can map these:

1) I have a field in my class of type File, I want the corresponding table of the class to have just the absolute path of this file stored as VARCHAR. Since FILE is a java class and it doesnt i do not know how I shud map it or have a seperate table for it?? This inbuilt file class has a getter for the path but not a setter.I cud just have a seperate String that stores the path and just map that.But they do not want an extra field storing the path. How else can I do it?

Thanx,
Shefali


You don't need to create a separate field for your absolute path, just getter and setter methods. Inside the Setter method you could create the File Object and inside the getter method you return the file.getAbsolutePath() value.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 3:52 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
I didnt quite get this line:

"Inside the Setter method you could create the File Object and inside the getter method you return the file.getAbsolutePath() value."
Will the getters and settrs return and accept object of type file. My class has a field of type file. I cannot just create it inside the setter. Its being used at many other places.

Also cud u give me an example of what the mapping file shud look like...

Thanx,
Shefali


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 4:08 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
shef wrote:
I didnt quite get this line:

"Inside the Setter method you could create the File Object and inside the getter method you return the file.getAbsolutePath() value."
Will the getters and settrs return and accept object of type file. My class has a field of type file. I cannot just create it inside the setter. Its being used at many other places.

Also cud u give me an example of what the mapping file shud look like...

Thanx,
Shefali


map the database attribute like this:

<property name="absolutePath" column="WHATEVER" type="string"/>

inside your java Class

Code:
public class YourClass {

  private File myFile;

  ... 
   // getter/setter for myFile go here
  ...
 
  public String getAbsolutePath() {
     if (myFile != null) {
       return myFile.getAbsolutePath() ;
     } else {
        return null;
     }
  }   

  public void setAbsolutePath(String absPath) {
     myFile = new File(absPath);
  }
}


You would of course need Exception handling arround the new File(...) but I hope you get the idea.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 6:01 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Hi,

Thanx a lot I solved my File problem.
However no one responded to my 2nd Question about the enums. Shud I making a new posting??

Shefali


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 7:36 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
There are several posting in the Wiki on this subject that show how to do this. In fact they show several solutions. Make sure you read the comments because many times people have suggested improvements over the original post. Here's a link to one of them.

http://www.hibernate.org/203.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 12:21 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Hi,

I have the following things in my class to map:

public class DataTupleHeader implements Serializable {

1) private Map<Integer,Feature>C = new HashMap<Integer, Feature>();

2) private EnumMap<FeatureType, Integer> FT = new
EnumMap<FeatureType, Integer>(FeatureType.class);

3) private LinkedList<TimePeriod> connections = new
LinkedList<TimePeriod>();
......
}

Here Feature, FeatureType, TimePeriod are user defined classes.
I am not sure what I shud be doing for each. For1) nad 3) shud I be using Map and List or shud I be jsut using a one-to-many association

Also the link you gave for Enum Maps only had examples of EnumMap of simple data types. But here its an EnumMap of type FeatureType( a class) and an Integer. It just seems kind of confusing to me.

I wud really appreciate if you cud help me get a better idea....feel a ltl lost here.

Thanks a lot,
Shefali


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 2:22 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
I don't know if all the generics will cause any problems but I'd handle number 3 as a list and number one as a map. I found a real simple example I had. The map1 class has a attribute data that contains a map of Integers to strings. The table RT_MAP2 then has 3 columns. The first 2 are ints and the third is a character. In your case since the things mapped are full persistent classes you'd have to replace the element many-to-many and you'd have another table. I've never actually done that but based on the manual entry it should work.

Code:
<hibernate-mapping>
   <class name="dor.utility.experiment.map1" table="RT_MAP1">
      <id column="ID" name="id" type="integer" unsaved-value="0" >
         <generator class="increment" />
      </id>   

      <property column="NAME" name="name" type="string" not-null="true" />   
      
      <map name="data" table="RT_MAP2" >
         <key column="ID" />
         
         <index column="KEY" type="java.lang.Integer" />

         <element column="DATA" type="string" />
      </map>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 26, 2005 2:35 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Hi,

Thanx for your reply. I have decided on the following mappings but am still trying the enumMap though.

1) private Map<Integer,Feature>C = new HashMap<Integer, Feature>();

<map name="C" table="cc">
<key column="ID"/>
<map-key column="KEY" type="integer"/>
<one-to-many entity-name="Feature"/>
</map>

Q1) here Map 'key column' is used for the mapping with Fetaure as a foreign key rt? I cannot get rid of it rt?
----------------------------------------------------------------------------------

3) private LinkedList<TimePeriod> connections = new
LinkedList<TimePeriod>();

<list name="connections" table="connections">
<key column="connectionId"/>
<list-index>
<column name="connectionIndex" not-null="true"/>
</list-index>
<composite-element class="TimePeriod">
<property name="period" type="long"/>
.........
</composite-element>
</list>
Q2) Can I get rid of either of "connectionIndex" or "connectionId" ...both are unique arent they. Dont quite understand why I need both here.
----------------------------------------------------------------------------------

Q3)I wud need two extra tables(cc, connections) one for storing the list elements, one for storing the Map elements + the tables for Feature and Time Period ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 26, 2005 3:02 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
A1). I'm not 100% sure I understand the question but no you can't get rid of it. That's the column that says which Features are in the map for the containing class.

A2). The connectionId identifies the list meaning it's the same for all rows that belong in the list for a particular instance of the containing class. The connectionIndex identifies the position of the item in the list.

A3). Yes.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 26, 2005 3:16 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
So it seems that my list and Map implementation are correct....

But I still cannot figure out how to map my EnumMap which is mapping from FeatureType(user defined class) to an Integer

EnumMap<FeatureType, Integer> f = new EnumMap<FeatureType, Integer>(FeatureType.class);

I looked at the links given I cudnt really figure it out... seems kinda complicated...can someone please guide me here.....

Thanx in advance
Shefali


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 26, 2005 3:48 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
In 1) my Q1) I wanted to know whether there was any difference between
<key column="ID"/>
<map-key column="KEY" type="integer"/>

and whether i needed both, I think I wud only need one foreign key in feature which will reference the primary key of the vehicle_id...what is the other for??

Also here in my Map if I use a one-to-many relationship i do not have the table for the map ..thats what it says in the hibernate reference...

table (optional - defaults to property name) the name of the collection table (not used for one-to-many associations)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 26, 2005 4:58 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
1). Basically the <key column="ID"> allows hibernate to find all the Feature instances in the map of a particular instance of vehicle. The <map-key column="KEY' type="integer"> is the key within the map that a particular instance of Feature is stored under. So you do need both.

I can think of 2 possible solutions.

1). Create a second getter and setter for the EnumMap attribute using a new name. The second getter would take the existing EnumMap and create a normal old map with Integers as keys and FeatureType as the value and return it. The second setter would accept a normal map and convert it to a EnumMap. Now you use this now propety for the mapping and map it as a normal Map.

2). The second option is a UserType. A UserType is a class you can name in a mapping document as the type for a property. The user type isn't the type for property as declared in the class. Instead it's a class that knows how to help Hibernate store the property in the database.

As you can see below I have a class with a property called name that is of a custom user developed type. Hibernate doesn't know how to store this in the database. So I create another class called a UserType that does know and I name it in the mapping document.

I've only done some very basic UserTypes so I can't really give you much help. What little I do know I've learned from studying the examples. If your in a hurry option is the way to go. Though you might want to learn something about UserTypes since they do allow you to handle some issues like this more elegantly.

Code:
      <property column="NAME" name="name" type="com.company.SomeUserType" not-null="true" >


Code:
public class Example {
   private FancyType name;

   public FancyType getName() {
      return(name);
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.