Hi,
it´s probably a very simple problem, but i can´t solve it.
I have a simple object mapped to a database-table. There are no relationships to other objects. I mapped the object via Annotations. Here is my Object-Class:
Code:
package de.gebit.geplan.datastructures.data;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;
@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 GeplanBenutzerDAO implements Serializable, IBenutzerDAO {
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 GeplanBenutzerDAO() {}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@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(((GeplanBenutzerDAO)o).getId());
}
}
When i create a new object and try to save it to the database, i got an error saying that null can not be inserted into field "b_nr". So the problem is that the id-field is not created correctly. What´s wrong with my approach and how can i fix it. "b_nr" is a numeric field and i just want him to take the next free number.
Thanks for any help.
Christoph
Hibernate version: 3.2.1
Name and version of the database you are using: Oracle 9i, MS SQL-Server 7.0