-->
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.  [ 10 posts ] 
Author Message
 Post subject: no polymorphic query
PostPosted: Tue Aug 09, 2005 5:03 am 
Newbie

Joined: Tue Aug 09, 2005 4:48 am
Posts: 6
I'm using Crietria object to build search queries but I need to performe searches without polymorphism, I have this hierarcy :

Admin extends Operator

when I look for all operators inside the system I don't want admins too.

How can I do this without using sqlRestriction ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 7:16 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
From a conceptual point of view, when you have inheritance association you have a "is" link. So an Admin "is" an Operator. So if you want all the operators you have the admins.

I don't think you can do what you want with hibernate hql. Maybe you can use a native sql query and use the discriminator value if you have one to filter the admins.

Seb


Top
 Profile  
 
 Post subject: Re: no polymorphic query
PostPosted: Tue Aug 09, 2005 8:52 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
patricknoir wrote:
I'm using Crietria object to build search queries but I need to performe searches without polymorphism, I have this hierarcy :

Admin extends Operator

when I look for all operators inside the system I don't want admins too.

How can I do this without using sqlRestriction ?


Without seeing your full mapping files, I would be hesitant to suggest this isn't possible. I would suggest however reading Section 10 of the R3.0.5 docs. It goes into great detail for inheritance strategies and their limitations.

_________________
Preston

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


Top
 Profile  
 
 Post subject: no polymorphic query
PostPosted: Tue Aug 09, 2005 10:19 am 
Newbie

Joined: Tue Aug 09, 2005 4:48 am
Posts: 6
Of course I know what hierarchy means, that an admin "is a" operator, but there are some cases that I need to look for operators that aren't admins. I got a solution adding a field specificRole and I use it like a discriminator, I know that it isn't a "clean" solution, probably I will use a sqlRestriction condition to check discriminator field if I won't find a way to avoid polymorphism

My O/R map file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="it.linksystem.csai.model.User" table="Users">
<id name="id" type="java.lang.Long">
<generator class="native"/>
</id>
<discriminator force="false" insert="true" not-null="false">
<column length="12" name="Role" not-null="true" sql-type="CHAR"/>
</discriminator>
<property name="phone"/>
<property name="password"/>
<property name="surname"/>
<property name="cellular">
<column name="cellular"/>
</property>
<property name="username">
<column name="username" not-null="true" unique="true"/>
</property>
<property name="email"/>
<property name="ac">
<column name="ac" not-null="true"/>
</property>
<property name="name"/>
<component name="address">
<property name="street"/>
<property name="cap">
<column name="cap"/>
</property>
<property name="city">
<column name="city"/>
</property>
<property name="number"/>
<property name="province"/>
<property name="country"/>
</component>
<bag name="societies" table="Employes" lazy="false">
<key>
<column name="user_id"/>
</key>
<many-to-many class="it.linksystem.csai.model.Society" column="society_id" lazy="false" />
</bag>
<property name="accountState"/>
<subclass discriminator-value="O" name="it.linksystem.csai.model.Operator">
<property name="role">
<column name="specificRole"/>
</property>
</subclass>
<subclass discriminator-value="T" name="it.linksystem.csai.model.Technician">
<property name="techLevel"/>
<bag name="zones" table="Turns" lazy="false">
<key>
<column name="technician_id"/>
</key>
<many-to-many class="it.linksystem.csai.model.Zone" column="zone_id"/>
</bag>
<property name="state"/>
</subclass>
<subclass discriminator-value="I" name="it.linksystem.csai.model.Inspector">
<property name="entity"/>
<bag name="reports" table="AssignedReports">
<key>
<column name="inspector_id" not-null="true"/>
</key>
<many-to-many class="it.linksystem.csai.model.ReportModel" column="report_id"/>
</bag>
</subclass>
<subclass discriminator-value="S" name="it.linksystem.csai.model.SuperUser"/>
<subclass discriminator-value="A" name="it.linksystem.csai.model.Admin">
<property name="role">
<column name="specificRole"/>
</property>
</subclass>
</class>
<class name="it.linksystem.csai.model.Society" table="Societies" lazy="true">
<id name="id">
<generator class="native"/>
</id>
<property name="phone"/>
<property name="maxTechnicians"/>
<property name="piva"/>
<property name="email"/>
<property name="maxOperators"/>
<property name="fax"/>
<property name="name"/>
<property name="maxElevators"/>
<property name="maxReports"/>
<property name="state"/>
<component name="address">
<property name="cap"/>
<property name="city"/>
<property name="country"/>
<property name="number"/>
<property name="province"/>
<property name="street"/>
</component>
</class>
<class name="it.linksystem.csai.model.search.Constraint" table="Constraints">
<id name="id">
<generator class="native"/>
</id>
<property name="operator"/>
<property name="propertyField"/>
<property name="propertySubField"/>
<property name="composite"/>
</class>
<class name="it.linksystem.csai.model.ReportModel" table="ReportModels">
<id name="id">
<generator class="native"/>
</id>
<many-to-one name="owner">
<column name="society"/>
</many-to-one>
<property name="name"/>
<property name="maxResults"/>
<property name="orderField"/>
<property name="startIndex"/>
<property name="subject"/>
<property name="paging"/>
<property name="ordered"/>
<property name="asc">
<column name="ascOrder" />
</property>
<bag name="constraints" table="ReportConstraints" lazy="false">
<key>
<column name="report_id"/>
</key>
<many-to-many class="it.linksystem.csai.model.search.Constraint" column="constraint_id" lazy="false" />
</bag>
</class>
<class name="it.linksystem.csai.model.Elevator" table="Elevators">
<id name="id">
<generator class="native"/>
</id>
<property name="brand"/>
<property name="sn"/>
<property name="entrance"/>
<property name="model"/>
<component name="address">
<property name="street"/>
<property name="province"/>
<property name="number"/>
<property name="country"/>
<property name="city"/>
<property name="cap"/>
</component>
<many-to-one name="society">
<column name="society"/>
</many-to-one>
<many-to-one name="zone">
<column name="zone"/>
</many-to-one>
<property name="buildingAdmin"/>
<property name="buildingAdminPhone"/>
<property name="contractExpiringDate"/>
<property name="keyholder"/>
<property name="keyholderPhone"/>
<property name="responsable"/>
<property name="responsablePhone"/>
<property name="state"/>
<many-to-one name="category"/>
</class>
<class name="it.linksystem.csai.model.Zone" table="Zones">
<id name="id">
<column name="id" unique="true"/>
<generator class="native"/>
</id>
<property name="country"/>
<property name="province"/>
<property name="name"/>
<property name="city"/>
<many-to-one name="society"/>
</class>
<class name="it.linksystem.csai.model.Breakdown" table="Breakdowns">
<id name="id">
<generator class="native"/>
</id>
<property name="insertDate"/>
<property name="endDate"/>
<bag name="handlers" table="BreakdownHandlers" lazy="false">
<key>
<column name="breakdown_id"/>
</key>
<many-to-many class="it.linksystem.csai.model.Operator" column="operator_id"/>
</bag>
<many-to-one name="technician"/>
<many-to-one name="inserter"/>
<many-to-one name="elevator">
<column name="elevator"/>
</many-to-one>
<many-to-one name="society">
<column name="society"/>
</many-to-one>
<property name="state"/>
<many-to-one name="type">
<column name="type"/>
</many-to-one>
<many-to-one name="techReport">
<column name="techReport"/>
</many-to-one>
</class>
<class name="it.linksystem.csai.model.TechReport" table="TechReports">
<id name="id">
<generator class="native"/>
</id>
<many-to-one name="breakdown">
<column name="breakdown"/>
</many-to-one>
<many-to-one name="technician"/>
<property name="description"/>
<property name="startTimeWorks"/>
<property name="note"/>
<property name="endTimeWorks"/>
</class>
<class name="it.linksystem.csai.model.BreakdownType" table="BreakdownTypes">
<id name="id">
<generator class="native"/>
</id>
<property name="description"/>
<many-to-one name="society">
<column name="society"/>
</many-to-one>
</class>
<class name="it.linksystem.csai.model.ElevatorCategory" table="ElevatorCategories">
<id name="id">
<generator class="native"/>
</id>
<property name="description"/>
<property name="signalationSystem"/>
<many-to-one name="society">
<column name="society"/>
</many-to-one>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: no polymorphic query
PostPosted: Tue Aug 09, 2005 10:27 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
patricknoir wrote:
Of course I know what hierarchy means, that an admin "is a" operator, but there are some cases that I need to look for operators that aren't admins. I got a solution adding a field specificRole and I use it like a discriminator, I know that it isn't a "clean" solution, probably I will use a sqlRestriction condition to check discriminator field if I won't find a way to avoid polymorphism

My O/R map file :


In looking at your mapping file, while Admin may extend Operator in the Java sense, it extends User with respect hibernate so you should be able to add polymorphism="explicit" to your class mapping and then, when you use
Code:
session.createCriteria(Operator.class).list();
You will only retrieve Operator instances.

If you had Admin extending Operator in the hibernate mappings, then this wouldn't work. the code would retrieve all Operator objects and subclasses.

Be sure to read about how this will effect the behavior in collections associated to other objects.

_________________
Preston

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


Top
 Profile  
 
 Post subject: not explicti hierarchy in hbm file
PostPosted: Tue Aug 09, 2005 12:32 pm 
Newbie

Joined: Tue Aug 09, 2005 4:48 am
Posts: 6
I didn't want to put explicit hierarchy in hbm mapping file between Operator and Admin hoping to be safe from polymorphic effect but maybe hibernate doesn't care so match about hbm file and use java to check polymorphism


Top
 Profile  
 
 Post subject: Re: not explicti hierarchy in hbm file
PostPosted: Tue Aug 09, 2005 1:08 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
patricknoir wrote:
I didn't want to put explicit hierarchy in hbm mapping file between Operator and Admin hoping to be safe from polymorphic effect but maybe hibernate doesn't care so match about hbm file and use java to check polymorphism


What I was saying is that because your Hibernate mappings don't have Admin extending Operator, you're createCriteria(Operator.class).list() will ONLY return Operator objects and not Admin Objects, which I think is what you're looking for.

_________________
Preston

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 1:33 pm 
Newbie

Joined: Tue Aug 09, 2005 4:48 am
Posts: 6
hat I was saying is that because your Hibernate mappings don't have Admin extending Operator, you're createCriteria(Operator.class).list() will ONLY return Operator objects and not Admin Objects, which I think is what you're looking for.

I think you didn't understand, I didn't put Admin extending Operator in my Hibernate file for the same reason that you have explained but it continues to give me Admins too in operator search.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 2:05 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
patricknoir wrote:
hat I was saying is that because your Hibernate mappings don't have Admin extending Operator, you're createCriteria(Operator.class).list() will ONLY return Operator objects and not Admin Objects, which I think is what you're looking for.

I think you didn't understand, I didn't put Admin extending Operator in my Hibernate file for the same reason that you have explained but it continues to give me Admins too in operator search.


You have to change your User class mapping to specify polymorphism="explicit"

Code:
<class name="test.polymorphism.User" table="Users" polymorphism="explicit">

_________________
Preston

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 11:26 pm 
Newbie

Joined: Tue Aug 09, 2005 4:48 am
Posts: 6
thanks pksiv, u were right, I forgot to put polymorphism explicit ;)


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