How to fix “Unable to find column with logical name” in Java, Hibernate

I’m getting the error “Unable to find column with logical name: dniNumeros in org.hibernate.mapping.Table(alumno) and its related supertables and secondary tables” when using IdClass

I have tried using an EmbeddedId instead and I’m getting the same error. The column obviously exists in my database, because the code works perfectly fine when I remove the IdClass line from it

@Entity
@Table(name="dificultadalumnoejercicio")
@IdClass(DificultadAlumnoEjercicioPK.class)
public class DificultadAlumnoEjercicio implements Serializable {

    private static final long serialVersionUID = -111454640507305257L;

    @Id
    @ManyToOne(cascade=CascadeType.MERGE)
    @JoinColumns ({
    @JoinColumn(name="alumnoDNINumeros", referencedColumnName="dniNumeros"),
    @JoinColumn(name="alumnoDNILetra", referencedColumnName="dniLetra")
    })
    private Alumno alumno;

    @Id
    @OneToOne(cascade=CascadeType.MERGE)
    @JoinColumn(name="ejercicioID", referencedColumnName="id")
    private Ejercicio ejercicio;

    @ManyToOne(cascade=CascadeType.MERGE)
    @JoinColumn(name="dificultadID", referencedColumnName="id")
    private Dificultad dificultad;

    // Constructors / Getters / Setters

}

And here’s my IdClass

@Embeddable
public class DificultadAlumnoEjercicioPK implements Serializable {

    private static final long serialVersionUID = 5559503979252689702L;

    public Alumno alumno;

    public Ejercicio ejercicio;

    // Constructors / Getters / Setters

}

Here is my database code:

CREATE TABLE `dificultadAlumnoEjercicio` (

    `alumnoDNINumeros` CHAR(8),
    `alumnoDNILetra` CHAR,
    `ejercicioID` INT,
    `dificultadID` INT NOT NULL,

    PRIMARY KEY (alumnoDNINumeros, alumnoDNILetra, ejercicioID),
    FOREIGN KEY (alumnoDNINumeros, alumnoDNILetra) REFERENCES alumno(dniNumeros, dniLetra),
    FOREIGN KEY (dificultadID) REFERENCES dificultad(id),
    FOREIGN KEY (ejercicioID) REFERENCES ejercicio(id)

);

CREATE TABLE `alumno` (

    `dniNumeros` CHAR(8),
    `dniLetra` CHAR,

    PRIMARY KEY (dniNumeros, dniLetra),
    FOREIGN KEY (dniNumeros, dniLetra) REFERENCES persona(dniNumeros, dniLetra)

);

I have already asked in Stackoverflow and nobody answered, and all my friends that are knowledgeable of Hibernate and they all say my code should work, so I don’t know where to look for the answer

If you can replicate it with this test case template and you think the mapping is supposed to work, then you should open a new Jira issue.

I have replicated the error in the test case template and in the process realized that it only happens when I have more than one @Id field in the DificultadAlumnoEjercicio class. If I were to remove the @Id from Ejercicio ejercicio then it would work. After this, I’m gonna assume this is some kind of internal hibernate bug and try to adapt my code so that I can do what I need. Thanks for the reply

If you assume it’s a bug, why don’t you create a Jira issue?

Because it’s a college project, I’m not working as part of a team

I was talking about the Hibernate Jira.

Oh, my bad, I thought you meant to report the bug to my organization or something.

Ok, so, here’s the thing, I got two friends of mine who hadn’t looked at the code before and they managed to fix it, but I don’t know if this is the correct way of working with Hibernate, and they’re not sure either.

What we did was explicitly declare all the table columns where needed, like


@Id 
@Column(insertable = false, updatable = false)
private String alumnoDNI;

or

@Id
@Column(insertable = false, updatable = false)
private int ejercicioId;

in addition of the implicit declaration of those columns that already was in the code through @JoinColumn annotations, and then we assign their value in the constructor, for example:

public DificultadAlumnoEjercicio(Alumno alumno, Ejercicio ejercicio, Dificultad dificultad) {
	
 super();
		
	this.alumno = alumno;
	this.ejercicio = ejercicio;
	this.dificultad = dificultad;
		
	this.alumnoDNI = alumno.getDni();
	this.ejercicioId = ejercicio.getId();
		
}

This works, but the fact that the program was working before without the IdClass line or without more than one Id makes me wonder if this is still abug (because it’s very inconsistent and the things that make the code work don’t make much sense from my point of view), so I’m unsure whether to report it as such. What do you think?

It’s hard tell whether it’s a bug or not unless I see all the code. Therefore, if you can replicate it, I will take a look at it.

Alright, here’s what I made. I tried to replicate the error I was having in the beginning, without the latest changes that I made to my project so you can see what I mean by it “working inconsistently”

What’s in the link is the script to create the db plus the project. If you try to run it as it is, you’ll get the error I was getting, “unable to find column with logical name…”

Try deleting the

@IdClass(ErrorPK.class)

line and it’ll work perfectly, and you’ll be able to add and delete from the db. This also happens if you delete the Id annotation from either alumn or randomIdColumn in Error.class. This is why I say this might be a bug, because when I delete code that has nothing to do with how the column is named or how it’s created, it suddenly works.

Link: https://github.com/DaveDarkLion/HibernateTest

  1. Alumn should use @MapsId. Check out this article for more details.
  2. ErrorPK should not use @Embeddable since it’s using @IdClass instead.

Thanks, I’ll change my code and see how it goes

very useful conversations.

https://wespm.com/category/notification/