-->
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.  [ 6 posts ] 
Author Message
 Post subject: findByExample including superclass
PostPosted: Tue Jun 27, 2006 9:39 am 
Newbie

Joined: Thu May 04, 2006 4:04 am
Posts: 5
hi!

i have a few tables in my database with fields which represent the same properties(for example id,lastModifiedBy and statusDeleted).in my java classes i want the following class hierarchy

DB looks like:
table1:
id,
lastModifiedBy

table2:
id
lastModifiedBy
statusDeleted


table3:
id
lastModifiedBy

table4
id
lastModifiedBy


and so on...

and my java classes look like

abstract class entity
private id
getters/setters

class modified extends entity
private lastModifiedBy
getters/setters

class deletable extends modified
private statusDeleted
getters/setters

class table2 extends deletable
priavte otherFields
getters/setters

class table4 extends modified
private otherFields
getters/setters

i changed nothing in the mapping files, because hibernate is able to set all the properties, because it finds the setters for all properties.
my problem is that, when i make a search by example and only set the id for my example(table4.setId=123), hibernate don't regard that the property id is set and searches for the whole table volume with where(1=1).
is there a way to tell hibernate, that the class table4 has a superclass(or two) which properties also are necessary??


if you don't clearly understand my problem, please contact me or answer my post that i can describe my problem in an other way for more understanding.
THANKS!

Hibernate version:
3.0
Name and version of the database you are using:
Oracle 9i


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 8:01 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Query by example ignores IDs. Have a look in the ref docs, section 15.6, "Example Queries". After all, if you have to ID, you'd use session.get(), not criteria.

Use normal criteria or HQL for this.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 3:46 am 
Newbie

Joined: Thu May 04, 2006 4:04 am
Posts: 5
but when i try the following:
Code:
Table4 example = new Table4();
example.setLastModifiedBy("someone");
List<Table4> result = session.createCriteria(Table4.class).add(Example.create(example)                                .ignoreCase().list();


hibernate doesn't check that the field lastModifiesBy of the superclass has changed and should be regarded in the generated select statement. (iwant a statement like: select id,... from Table4 where lastModifiedBy=? and not select id,... from Table4 where(1=1))
so is there no way to tell hibernate that there are also superclasses of my object which include fields for my select statement?
if this is the case, then I only can use HQL for this!?

thanks for replies!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 5:03 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
As far as I know, non-ID properties of mapped superclasses are used in example queries. How have you mapped the relationship between Table1 and Table4? Are you using hibernate's polymorphic mapping features (<subclass>, <union-subclass> or <joined-subclass>)? Hibernate doesn't care that one class extends another: the class' mapping must extend the other class' mapping for hibernate to consider it.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 3:59 am 
Newbie

Joined: Thu May 04, 2006 4:04 am
Posts: 5
there is no relationship between table1 and table4, maybe i expressed my problem not exactly enough.

table4 in db
+id
+lastModifiedBy
+field1
+field2
+field3

hibernate has generated a mapping like:
<class name"Table4" table=table4" schema="someSchema">
<id name="id" type=long">
<column name="id" precision="12" scale="0" />
<generator class="assigned" />
</id>

<property name="lastModifiedBy" type="String">
<column name="lastModifiedBy" length="20" />
</property>

<property name="field1String" type="String">
<column name="field1" length="20" />
</property>

<property name="field2String" type="String">
<column name="field2" length="20" />
</property>

<property name="field3String" type="String">
<column name="field3" length="20" />
</property>


my java classes:
Code:
public class Entity{
   protected long id;

   public long getId(){
      return id;
   }

   public void setId(long id){
      this.id=id;
   }
}

public class Modifiable extends Entity{
   protected String lastModifiedBy;

   public String getLastModifiedBy(){
      return this.lastModifiedBy;
   }

   public void setLastModifiedBy(String lastModifiedBy){
      this.lastModifiedBy=lastModifiedBy;
   }
}

public class Table4 extends Modifiable {

   private String field1String;
   private String field2String;
   private String field3String;

   public String getField1String() {
      return field1String;
   }

   public void setField1String(String field1String) {
      this.field1String = field1String;
   }

   public String getField2String() {
      return field2String;
   }

   public void setField2String(String field2String) {
      this.field2String = field2String;
   }

   public String getField3String() {
      return field3String;
   }

   public void setField3String(String field3String) {
      this.field3String = field3String;
   }
}


i hope that it is now more clearly. where do i have to tell(and how) hibernate the subclass mapping, so that i can make an example search. like i have described before:
Quote:
Table4 example = new Table4();
example.setLastModifiedBy("someone");
List<Table4> result = session.createCriteria(Table4.class).add(Example.create(example) .ignoreCase().list();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 5:57 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
lastModifiedBy is in the mapping for Table4, so it should be checked in the example query. It doesn't matter that the method is in a java superclass, what's important is that it's in the mapping (or the mapping's super-mapping, if it's a polymorphic mapping, which it's not in this case). I think that the query you wrote should work (assuming that there's a second close-paranthesis after the ignoreCase()).

Can you get any property to be checked in the example query? If you call setField2String("string"), does the query work? I suggest tweaking your code to narrow down the cause of the problem, because it looks all good to me.

_________________
Code tags are your friend. Know them and use them.


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