-->
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.  [ 3 posts ] 
Author Message
 Post subject: query results in wrong number of rows
PostPosted: Thu Jan 04, 2007 12:47 pm 
Newbie

Joined: Wed May 04, 2005 9:34 am
Posts: 16
Location: Germany
Hi

i´m using Hibernate with Spring and i have the following problem: I have a very simple database with one table. The corresponding domain-object mapped to the database with annotations looks like this

Code:
@Entity
@Table(name="S_Benutzer",
   uniqueConstraints = {@UniqueConstraint(columnNames={"b_nr"})})
@org.hibernate.annotations.Table(appliesTo="S_Benutzer", indexes={@Index(name="b_nr", columnNames={"b_nr"})})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@AccessType("property")
public class BenutzerDOM implements Serializable, IBenutzerDOM {
   private static final long serialVersionUID = 1L;
   private Long id;
   private String name;
   private String vorname;
   private int art;
   private String kennung;
   private String passwort;
   private int sperre;
   private int standard;
      
   public BenutzerDOM() {}

   @Id 
        @GenericGenerator(name="pk_creator", strategy = "increment")
   @GeneratedValue(generator="pk_creator")
   @Column(name="b_nr", nullable=false, unique=true, length=4)
   public Long getId() {
      return id;
   }

   @Basic
   @Column(name="b_name", length=200)
   public String getName() {
      return name;
   }

   @Basic
   @Column(name="b_vname", length=200)
   public String getVorname() {
      return vorname;
   }

   @Basic
   @Column(name="b_kenn", length=200)
   public String getKennung() {
      return kennung;
   }

   @Basic
   @Column(name="b_passwort", length=200)
   public String getPasswort() {
      return passwort;
   }

   @Basic
   @Column(name="b_art", length=2)
   public int getArt() {
      return art;
   }

   @Basic
   @Column(name="b_sperre", length=200)
   public int getSperre() {
      return sperre;
   }

   @Basic
   @Column(name="b_standard", length=2)
   public int getStandard() {
      return standard;
   }

   public void setId(Long id) {
      this.id=id;
   }
   
   public void setName(String name) {
      this.name=name;
   }

   public void setVorname(String name) {
      this.vorname=name;
   }

   public void setKennung(String kennung) {
      this.kennung=kennung;
   }

   public void setPasswort(String passwort) {
      this.passwort=passwort;
   }

   public void setArt(int art) {
      this.art=art;
   }
   
   public void setSperre (int sperre) {
      this.sperre=sperre;
   }

   public void setStandard(int pass_change) {
      this.standard=pass_change;
   }

   public int compareTo(Object o) {
      return this.getId().compareTo(((BenutzerDOM)o).getId());
   }

   @Override
   public boolean equals(Object obj) {
      if (obj==this) return true;
      if((obj == null) || (obj.getClass() != this.getClass())) return false;
      
      BenutzerDOM benutzer=(BenutzerDOM)obj;
      boolean rueck=true;
      
      rueck=rueck && (name==null ? benutzer.name==null : name.equals(benutzer.name));
      rueck=rueck && (vorname==null ? benutzer.vorname==null : vorname.equals(benutzer.vorname));
      rueck=rueck && (art==benutzer.art);
      rueck=rueck && (kennung==null ? benutzer.kennung==null : kennung.equals(benutzer.kennung));
      rueck=rueck && (passwort==null ? benutzer.passwort==null : passwort.equals(benutzer.passwort));
      rueck=rueck && (sperre==benutzer.sperre);
      rueck=rueck && (standard==benutzer.standard);
      
      // Rückgabewert
      return rueck;
   }

   @Override
   public int hashCode() {
      int hash=7;
      
      hash=31*hash+(name!=null ? name.hashCode() : 0);
      hash=31*hash+(vorname!=null ? vorname.hashCode() : 0);
      hash=31*hash+art;
      hash=31*hash+(kennung!=null ? kennung.hashCode() : 0);
      hash=31*hash+(passwort!=null ? passwort.hashCode() : 0);
      hash=31*hash+sperre;
      hash=31*hash+standard;
      
      return hash;      
   }
}


In the table are 6 records with the id ranging from 1 to 6. Now i try to load these records and i use the HibernateTemplate from Spring. Here i create a DetachedCriteria and pass it to the method findByCriteria(). That looks like this:

Code:
DetachedCriteria criteria=DetachedCriteria.forClass(BenutzerDOM.class);
criteria.addOrder(Order.asc("id"));

List result=hibernateTemplate.findByCriteria(criteria);


The problem is, that my result has 7 records instead of 6 and the last record (which is not in the database) has the id 10 (every other field is null). There is no record with id 10 in the database.

Whats wrong here? Can anybody help me?

Thanks in advance for anyhelp.

Christoph
Hibernate version: 3.2

Name and version of the database you are using: MS SQL-Server 7.0

The generated SQL (show_sql=true):
select this_.b_nr as b1_6_0_, this_.b_name as b2_6_0_, this_.b_kenn as b3_6_0_, this_.b_passwort as b4_6_0_, this_.b_art as b5_6_0_, this_.b_sperre as b6_6_0_, this_.b_vname as b7_6_0_, this_.b_standard as b8_6_0_ from S_Benutzer this_ order by this_.b_nr asc


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 1:30 pm 
Regular
Regular

Joined: Thu Aug 17, 2006 4:50 am
Posts: 55
Location: Mallorca
I dont know if this could be the problem but your equals method is incorrect.

I had the same problem with the equals method generated by the intelliJ.

Look at the line :

Code:
if((obj == null) || (obj.getClass() != this.getClass())) return false;


Using obj.getClass() and this.getClass() can fail with proxy classes.

Use instead :

Code:
if (! (obj instanceof <your_class>) ) return false;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 3:58 am 
Newbie

Joined: Wed May 04, 2005 9:34 am
Posts: 16
Location: Germany
thanks very much for the hint, but that wasn´t the problem here.

By the way it also doesn´t work, when i use hql and also when i ommit Spring and use pure Hibernate like this:

Code:
Session s=hibernateTemplate.getSessionFactory().openSession();
List result=s.createQuery("from BenutzerDOM").list();


i get 7 records in my resultset. And it´s definetly the correct database i´m using. What´s wrong here?


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