-->
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.  [ 4 posts ] 
Author Message
 Post subject: HowTo map class myClass : List<MyOtherClass>.....?
PostPosted: Mon Dec 22, 2008 6:41 pm 
Newbie

Joined: Mon Dec 22, 2008 6:32 pm
Posts: 5
Hi all,

I searched this forum, but i did not find an answer, so I try this. Hope you have an idea how I can do this mapping.

I have 2 classes:
Code:
public class Class1 : List<Class2>{

}

public class Class2{

}


I want to persist Class1. Can you give me a hint how to write the mapping for this construction?

I have read the hibernate doc, but there is no such example or case.

Thx

Alex


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 10:04 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I think that's not possible, you have to put List<Class1> as a member/property in Class2.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 12:59 pm 
Newbie

Joined: Mon Dec 22, 2008 6:32 pm
Posts: 5
I suspected that :-(

But thx for your answer.

For all others who have the same problem try this:
Code:
public class Class1 : IList {
private List<Class2> _Class2List

//Implement the Interface List and map the access from extern to your inner list

}


class Class2{
}


In the nhibernate doc in chapter 19.2 there is a nice example. You can look there for a adequate example.

If I have implemented my mapping I will post here my complete solution. Some time after christmas :-)

And if someone else has a good idea how to map my initial class, please let me know.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 28, 2008 9:01 pm 
Newbie

Joined: Mon Dec 22, 2008 6:32 pm
Posts: 5
Here a full solution how you can do this:

Code:
public class Class1 : List<Class2>
    {
        private int _ID;

        public virtual int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public virtual List<Class2> Class2List
        {
            get
            {
                return this;
            }
            set
            {
                Clear();
                this.AddRange(value);
            }
        }
    }

public class Class2
{
     
}



And the mapping for Class1:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" namespace="HiberTest" assembly="HiberTest">
  <class name="HiberTest.Class1,HiberTest" table="class1" lazy="false">
   
    <id name="ID" column="ID">
      <generator class="native" />
    </id>

    <list name="Class2List" table="class2" lazy="true">
      <key column="Class1_ID" />
      <index column="IndexCol"/>
      <composite-element class="HiberTest.Class2"/>
    </list>
   
  </class>

</hibernate-mapping>


The code created from Schemaexport looks like this:

Code:
alter table class2  drop foreign key FK151EA7672B4620AC

drop table if exists class1
drop table if exists class2
create table class1 (
  ID INTEGER NOT NULL AUTO_INCREMENT,
   primary key (ID)
)
create table class2 (
  Class1_ID INTEGER not null,
   IndexCol INTEGER not null,
   primary key (Class1_ID, IndexCol)
)
alter table class2 add index (Class1_ID), add constraint FK151EA7672B4620AC foreign key (Class1_ID) references class1 (ID)



Because Class2 is empty you don't need a mapping file. But normally the class won't be empty, so you can simple create a mapping file for Class2.

One thing that is important is that you disable lazy loading. In this example Class1 inherits from a List. So Class1 inherits a lot of methods which are not virtual. If you have lazy loading enabled you will get an exception. What you can do is, you can override all these inherited methods and make them virtual.

Or you do not inherit from a List and implement this with an inner list.

One thing in the end: Don't name your index column "Index". I used MySQL and got an error. Index is a reserved word.


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