0

I have a rather weird case. I have some entities generated with netbeans and i can persist all except one. I see no difference in the database nor in the entity class. Can someone help me, here is my entity class , database, and error that i am receiving

CREATE TABLE objekat
(
  id_objekat bigserial NOT NULL,
id_opstina serial NOT NULL,
naziv character varying(50) NOT NULL,
kapacitet character varying(50),
adresa character varying(100),
lokacija_sirina double precision,
lokacija_duzina double precision,
opis character varying(500),
korisnicko_ime character varying(50),
sifra character varying(50),
maks_broj_slike integer,
absolute_path_logo character varying(255),
CONSTRAINT objekat_pkey PRIMARY KEY (id_objekat),
CONSTRAINT fkobjekat924176 FOREIGN KEY (id_opstina)
  REFERENCES opstina (id_opstina) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
 OIDS=FALSE
);

and this is my entity bean.

@Entity
@Table(name = "objekat")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Objekat.findAll", query = "SELECT o FROM Objekat o"),
@NamedQuery(name = "Objekat.findByIdObjekat", query = "SELECT o FROM Objekat o WHERE o.idObjekat = :idObjekat"),
@NamedQuery(name = "Objekat.findByNaziv", query = "SELECT o FROM Objekat o WHERE upper (o.naziv) like upper(:naziv)"),
@NamedQuery(name = "Objekat.findByNazivAndOpstina", query = "SELECT o FROM Objekat o inner join o.idOpstina op WHERE upper (o.naziv) like upper(:naziv) and op.idOpstina = :idOpstina"),
@NamedQuery(name = "Objekat.findByKapacitet", query = "SELECT o FROM Objekat o WHERE o.kapacitet = :kapacitet"),
@NamedQuery(name = "Objekat.findByAdresa", query = "SELECT o FROM Objekat o WHERE o.adresa = :adresa"),
@NamedQuery(name = "Objekat.findByLokacijaSirina", query = "SELECT o FROM Objekat o WHERE o.lokacijaSirina = :lokacijaSirina"),
@NamedQuery(name = "Objekat.findByLokacijaDuzina", query = "SELECT o FROM Objekat o WHERE o.lokacijaDuzina = :lokacijaDuzina")})
public class Objekat implements Serializable {


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_objekat",columnDefinition = "BIGSERIAL")
private Long idObjekat;

@Size(max = 255)
@Column(name = "absolute_path_logo")
private String absolutePathLogo;
@OneToMany( mappedBy = "objekatidObjekat")
private List<DogadjajObjekat> dogadjajObjekatList;
@OneToMany( mappedBy = "objekatidObjekat")
private List<SlikeLokacijaObjekat> slikeLokacijaObjekatList;
@OneToMany( mappedBy = "idObjekat")
private List<RasporedObjekat> rasporedObjekatList;
@Column(name = "maks_broj_slike")
private Integer maksBrojSlike;
@Size(max = 50)
@Column(name = "korisnicko_ime")
private String korisnickoIme;
@Size(max = 50)
@Column(name = "sifra")
private String sifra;
@Size(max = 500)
@Column(name = "opis")
private String opis;
// @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "lokacija_sirina")
private Double lokacijaSirina;
@Column(name = "lokacija_duzina")
private Double lokacijaDuzina;



@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "naziv")
private String naziv;
@Size(max = 50)
@Column(name = "kapacitet")
private String kapacitet;
@Size(max = 100)
@Column(name = "adresa")
private String adresa;
@JoinTable(name = "tip_objekta_objekat", joinColumns = {
    @JoinColumn(name = "objekatid_objekat", referencedColumnName = "id_objekat")}, inverseJoinColumns = {
    @JoinColumn(name = "tip_objektaid_tip_objekta", referencedColumnName = "id_tip_objekta")})
@ManyToMany
private List<TipObjekta> tipObjektaList;
@JoinColumn(name = "id_opstina", referencedColumnName = "id_opstina")
@ManyToOne(optional = false)
private Opstina idOpstina;

public Objekat() {
}

public Objekat(Long idObjekat) {
    this.idObjekat = idObjekat;
}

public Objekat(Long idObjekat, String naziv) {
    this.idObjekat = idObjekat;
    this.naziv = naziv;
}

public Long getIdObjekat() {
    return idObjekat;
}

public void setIdObjekat(Long idObjekat) {
    this.idObjekat = idObjekat;
}

public String getNaziv() {
    return naziv;
}

public void setNaziv(String naziv) {
    this.naziv = naziv;
}

public String getKapacitet() {
    return kapacitet;
}

public void setKapacitet(String kapacitet) {
    this.kapacitet = kapacitet;
}

public String getAdresa() {
    return adresa;
}

public void setAdresa(String adresa) {
    this.adresa = adresa;
}



@XmlTransient
public List<TipObjekta> getTipObjektaList() {
    return tipObjektaList;
}

public void setTipObjektaList(List<TipObjekta> tipObjektaList) {
    this.tipObjektaList = tipObjektaList;
}




public Opstina getIdOpstina() {
    return idOpstina;
}

public void setIdOpstina(Opstina idOpstina) {
    this.idOpstina = idOpstina;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (idObjekat != null ? idObjekat.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Objekat)) {
        return false;
    }
    Objekat other = (Objekat) object;
    if ((this.idObjekat == null && other.idObjekat != null) || (this.idObjekat != null && !this.idObjekat.equals(other.idObjekat))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "rs.dzetSet.entiteti.Objekat[ idObjekat=" + idObjekat + " ]";
}

public String getOpis() {
    return opis;
}

public void setOpis(String opis) {
    this.opis = opis;
}

public Double getLokacijaSirina() {
    return lokacijaSirina;
}

public void setLokacijaSirina(Double lokacijaSirina) {
    this.lokacijaSirina = lokacijaSirina;
}

public Double getLokacijaDuzina() {
    return lokacijaDuzina;
}

public void setLokacijaDuzina(Double lokacijaDuzina) {
    this.lokacijaDuzina = lokacijaDuzina;
}


public String getKorisnickoIme() {
    return korisnickoIme;
}

public void setKorisnickoIme(String korisnickoIme) {
    this.korisnickoIme = korisnickoIme;
}

public String getSifra() {
    return sifra;
}

public void setSifra(String sifra) {
    this.sifra = sifra;
}


public Integer getMaksBrojSlike() {
    return maksBrojSlike;
}

public void setMaksBrojSlike(Integer maksBrojSlike) {
    this.maksBrojSlike = maksBrojSlike;
}

public void pocevajMaksBrojSlike(){
    this.maksBrojSlike++;
}

public String getAbsolutePathLogo() {
    return absolutePathLogo;
}

public void setAbsolutePathLogo(String absolutePathLogo) {
    this.absolutePathLogo = absolutePathLogo;
}

@XmlTransient
public List<RasporedObjekat> rasporedObjekatListPrePodne(){
List<RasporedObjekat> rez = new ArrayList<RasporedObjekat>();
if(rasporedObjekatList==null){
    rasporedObjekatList = new ArrayList<RasporedObjekat>();
}
for(RasporedObjekat ro:rasporedObjekatList){
    if(!ro.getVecernjiProgram()){
        rez.add(ro);
    }
}
return rez;
}


@XmlTransient
public List<RasporedObjekat> rasporedObjekatListPoslePodne(){
List<RasporedObjekat> rez = new ArrayList<RasporedObjekat>();
if(rasporedObjekatList==null){
    rasporedObjekatList = new ArrayList<RasporedObjekat>();
}
for(RasporedObjekat ro:rasporedObjekatList){
    if(ro.getVecernjiProgram()){
        rez.add(ro);
    }
}
return rez;
}

@XmlTransient
public List<DogadjajObjekat> getDogadjajObjekatList() {
    return dogadjajObjekatList;
}

public void setDogadjajObjekatList(List<DogadjajObjekat> dogadjajObjekatList) {
    this.dogadjajObjekatList = dogadjajObjekatList;
}

@XmlTransient
public List<SlikeLokacijaObjekat> getSlikeLokacijaObjekatList() {
    return slikeLokacijaObjekatList;
}

public void setSlikeLokacijaObjekatList(List<SlikeLokacijaObjekat> slikeLokacijaObjekatList) {
    this.slikeLokacijaObjekatList = slikeLokacijaObjekatList;
}

@XmlTransient
public List<RasporedObjekat> getRasporedObjekatList() {
    return rasporedObjekatList;
}

public void setRasporedObjekatList(List<RasporedObjekat> rasporedObjekatList) {
    this.rasporedObjekatList = rasporedObjekatList;
}

and i persist it in a rather normal way, or i just think so.

utx.begin();
           if(noviObjekat.getIdObjekat() == null){
               em.persist(noviObjekat);
           }else{
               em.merge(noviObjekat);
           }
utx.commit();

and i get a pretty weird error

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):   org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: null value in column "id_opstina" violates not-null constraint
Error Code: 0
Call: INSERT INTO objekat (absolute_path_logo, adresa, kapacitet, korisnicko_ime, lokacija_duzina, lokacija_sirina, maks_broj_slike, naziv, opis, sifra, id_opstina) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [11 parameters bound]
Query: InsertObjectQuery(rs.dzetSet.entiteti.Objekat[ idObjekat=null ])

}

1 Answer 1

1

You set a generator on the field for "id_objekat" but the exception is for the not-null constraint on "id_opstina". You will need to set this field yourself or use a returning policy to get it instead: http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Extensions_for_Returning_Policy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.