Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:00:19+00:00 2026-06-13T00:00:19+00:00

I’m starting to use JPA with the OpenJPA API, and i’m having a problem

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:00:20+00:00Added an answer on June 13, 2026 at 12:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.