JPA (Hibernate): error when using @EmbeddedId in generic @MappedSuperclass

I am currently defining JPA objects for an outdated database (many composite keys, but also single-column keys). I created the following superclass of the class:

@MappedSuperclass
public abstract class AbstractEntity<ID extends Serializable> {
    public abstract ID getId();
    public abstract void setId(ID id);
}

And then the superclass for compound keys (as well as the superclass for the long primary key not specified here):

@MappedSuperclass
public abstract class AbstractEmbeddedIdEntity<ID extends Serializable> extends AbstractEntity<ID> {
    @EmbeddedId
    private ID id;

    public AbstractEmbeddedIdEntity() {
        id = newId();
    }

    @Override
    public ID getId() {
        return id;
    }

    @Override
    public void setId(ID id) {
        this.id = id;
    }

    protected abstract ID newId();
}

And finally, specific objects like this:

@Entity
@Table(name = "firstEntity")
public class FirstEntity extends AbstractEmbeddedIdEntity<FirstEntityId> {

    public FirstEntity() {
    }

    @Embeddable
    public static class FirstEntityId implements Serializable {
        @Column(name = "firstId")
        private String firstId;

        public FirstEntityId() {
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (!(obj instanceof FirstEntityId)) {
                return false;
            }
            FirstEntityId other = (FirstEntityId) obj;
            return 
                    Objects.equals(firstId, other.firstId);
        }

        @Override
        public int hashCode() {
            return Objects.hash(firstId);
        }
    }

    @Override
    protected FirstEntityId newId() {
        return new FirstEntityId();
    }
}

Now the problem is that if I have several objects like this and try to access the property of the object identifier (currently with Spring Boot, for example findByIdFirstId(String firstId)), an exception is thrown:

java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [firstId] on this ManagedType [unknown]

, MappedSupperclass. @EmbeddedId, newId(), MappedSupperclass, . , MappedSupper, MappedSupperclass @EmbeddedId .

ID , @EmbeddedId firstId ( ).

, , - ?

, Spring boot github. mvn spring-boot:run.

+4
1

, hibernate bug tracker. ID (@EmbeddedId) .

0

Source: https://habr.com/ru/post/1684654/


All Articles