-->
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: problem loading interfaces
PostPosted: Fri Sep 18, 2009 8:19 am 
Newbie

Joined: Fri Sep 18, 2009 7:58 am
Posts: 4
Hi, I have 3 tables in our db.
TABLE: Node(ID, StatusId, Type)
1, 1, S
2, 1, E

TABLE: Start(startID, departure)
1 10:00

TABLE: End(endID, arrival)
1 18:00
Code:
public class Node {
   int nodeId;
   Status status;
}
publilc interface Status {
  // marker interface
}
public class Start implements Status {
   int startId;
   Time departure;
}
public class End implements Status {
   int endId;
   Time arrival;
}

How to map status in node.hbm.xml based on the NodeType


Last edited by kattavijay on Sun Sep 20, 2009 11:14 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: problem loading interfaces
PostPosted: Sun Sep 20, 2009 4:35 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi kattavijay,

I think you have to make Status an entity - map it itself. Then, it can be referenced by other entities. I guess you'd need a reference to an existing instance which will be of the type of an implementing class. You might - if possible - turn the interface into an abstract class and let it do the ID handling for all of its sub-classes.

CU
Froestel

P.S.: It would be very nice if you'd enclose your code snippets in BBCode Code-Tags (see the buttons below the subject field) for better readability.

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: problem loading interfaces
PostPosted: Sun Sep 20, 2009 5:44 am 
Newbie

Joined: Fri Sep 18, 2009 7:58 am
Posts: 4
Hi,
the implementations of Status.java generate their own ids. the ids are generated by different front-ends, so I can not move id to Status.java. The only way to load status is based on the field nodetype.


Top
 Profile  
 
 Post subject: Re: problem loading interfaces
PostPosted: Sun Sep 20, 2009 6:21 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi kattavijay,

if you require shared references to single instances of Status map the class to become an entity. Otherwise you might map it as a <component> (see Hibernate documentation, Sec. 5.1.15 http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html#mapping-declaration-component and Sec. 8 http://docs.jboss.org/hibernate/stable/core/reference/en/html/components.html) of your Node entity.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: problem loading interfaces
PostPosted: Sun Sep 20, 2009 6:53 am 
Newbie

Joined: Fri Sep 18, 2009 7:58 am
Posts: 4
Hi,
thanks for the reply.
My problem is not whether to map Status as entity or component. but how to load that entity/component. i mean based on the NodeType hibernate has to instantiate and load either Start or End from their respective tables.


Top
 Profile  
 
 Post subject: Re: problem loading interfaces
PostPosted: Sun Sep 20, 2009 10:29 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi kattavijay,

Quote:
load either Start or End from their respective tables


Ah, I think now I understood your problem.

You need to map Start and End as sub-classes of Status. There are three strategies provided (see http://docs.jboss.org/hibernate/stable/core/reference/en/html/inheritance.html). I guess - as long as there are no additional classes extending Start and End any further (which is also possible, don't get me wrong) - then Table per subclass looks appropiate for your case.

A <many-to-one> mapping with the Node mapping targeting the Status entity should do it. Hibernate takes care of the polymorphism stuff then.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: problem loading interfaces
PostPosted: Sun Sep 20, 2009 11:13 am 
Newbie

Joined: Fri Sep 18, 2009 7:58 am
Posts: 4
Hi,
I can't use table per sub-class strategy as I don't have a status table.
I only have Start and End tables with their own id generators i.e. both the tables could have same ids.
I think I could use table per concrete class (using implicit polymorphism) strategy. so I have a mapping like this. But,

<class name="Start" table="Start">
<id name="id" type="int" column="startID">
<generator class="native"/>
</id>
<property name="departure" type="String" column="departure"/>
</class>

<class name="End" table="End">
<id name="id" type="int" column="endID">
<generator class="native"/>
</id>
<property name="arrival" type="String" column="arrival"/>
</class>

<class name="Node" table="Node">
<id name="id" type="int" column="ID">
<generator class="native"/>
</id>
<property name="nodeType" type="String" column="Type"/>
<many-to-one name="status" column="statusID" class="Status">
<!-- here i want to load status based on property nodeType i.e. to load Start.java (where Type="Start") -->
</class>

I dont necessarily need to load Status.java, is it possible to load Start.java for Type="Start" and End.java for Type="End"


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.