Search code examples
javajpaopenjpa

JPA find() does not load Entity @Id


I'm starting to use JPA with the OpenJPA API, and i'm having a problem with the find(). Here are the tables:

CREATE TABLE compania (
  ID int(11) NOT NULL,
  NOMBRE varchar(45) DEFAULT NULL,
  PRIMARY KEY (ID)
) 

CREATE TABLE modelo (
  ID int(11) NOT NULL,
  ID_COMPANIA int(11) DEFAULT NULL,
  NOMBRE_MODELO varchar(45) DEFAULT NULL,
  PRIMARY KEY (ID),
  KEY MODELO_COMPANIA_FK_idx (ID_COMPANIA),
  CONSTRAINT MODELO_COMPANIA_FK FOREIGN KEY (ID_COMPANIA) REFERENCES compania (ID)
) 

and here are my Entities:

@Entity
public class Compania extends EntityJPA{

    private static final long serialVersionUID = 1L; 

    @Id
    private int id;

    @Column
    private String nombre;

    @OneToMany(mappedBy="compania",cascade={CascadeType.ALL})
    @JoinColumn(name="ID_COMPANIA", nullable=false)
    private List<Modelo> listaModelos;

    public Compania() {
    }

    public int getId() {
        return id;
    }

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

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombreCompania) {
        this.nombre = nombreCompania;
    }

    public List<Modelo> getListaModelos() {
        return listaModelos;
    }

    public void setListaModelos(List<Modelo> listaModelos) {
        this.listaModelos = listaModelos;
    }
}

@Entity
public class Modelo extends EntityJPA{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name="NOMBRE_MODELO")
    private String nombreModelo;

    @ManyToOne
    @JoinColumn(name="ID_COMPANIA", referencedColumnName="ID")
    private Compania compania;

    public Modelo() {
    }

    public Compania getCompania() {
        return compania;
    }

    public void setCompania(Compania compania) {
        this.compania = compania;
    }

    public int getId() {
        return id;
    }

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

    public String getNombre() {
        return nombreModelo;
    }

    public void setNombre(String nombreModelo) {
        this.nombreModelo = nombreModelo;
    }
}

At the moment I make the

Compania cia = getEntityManager().find(Compania.class, idCompania);

the cia object does not have the value of the @Id attribute, it has the value of nombre but not of id. I mean:

cia.getId() = 0

and it must be 1 or 2 , etc. Not 0.

Thank you very much for your help.

I do not have the code to persist because It was already persisted. the code for the find is

public static Compania findCompania(int idCompania){
        try {
            Compania cia = getEntityManager().find(Compania.class, idCompania);
            return cia;
        } finally {
            closeEntityManager();
        }
    }

And if I activate the log, this is the select it shows:

482  testMySql  TRACE  [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 1228180882, conn 1699837157> executing prepstmnt 2127861376 SELECT t0.nombre FROM Compania t0 WHERE t0.id = ? [params=(int) 1]
497  testMySql  TRACE  [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 1228180882, conn 1699837157> [15 ms] spent

As you can see, there is no t0.id in the select.

Thanks for your help.


Solution

  • Primary Key (ID) not retrieved (?) from database using OpenJPA

    Duplicate.... the net of the post is that you need to use a different enhancement method.