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