Hibernate session.get requires fully qualified class name, despite explicit entity name

I’m using Hibernate 6, and I have some code I’ve been porting that uses Session.get with an entity name and identifier. We do a lot of reflection, and there’s some other background, so this may look a bit odd and explicitly passing the entity name may seem superfluous, but it’s necessary for us.

The entity name by itself doesn’t work, it seems to require the fully qualified name:

@Entity(name = "Root")
...
session.get("Root", rootId); // doesn't work
...
session.persist("Root", root); //works...???

For the session.get, I end up with a

org.hibernate.UnknownEntityTypeException: Unable to locate persister: Root

However,

session.get("mypackage.fully.qual.Root", rootId); // works

How do you map that entity? hbm.xml? Share the mapping please.

It’s an annotated POJO. That’s the entity annotation at the top of the code block, with the explicit name. It used to be hbm.xml mappings, and in that older version of Hibernate, we didn’t have issues with passing the entity name to get like this.

I need to see the entity mappings to be able to help you.

Gotcha…thought it was just that POJO annotation you were looking for.

The mappings are like this (some brevity so hopefully this is all the relevant parts)

package mypackage.fully.qual;

@Entity(name = "Root")
@Table(name = "ROOT")
public class Root extends EntityBase implements java.io.Serializable {
    private static final long serialVersionUID = 1743656930;
    
    private String rootName;
    private List<UniqueCaseRecord> uniqueCaseRecords;

    @NaturalId
    @Column(name = "ROOT_NAME")
    public String getRootName() {
        return this.rootName;
    }

    public void setRootName(String rootName) {
        this.rootName = rootName;
    }
    
    @OneToMany(cascade = jakarta.persistence.CascadeType.ALL, mappedBy = "root")
    public List<UniqueCaseRecord> getUniqueCaseRecords() {
        return this.uniqueCaseRecords;
    }

    public void setUniqueCaseRecords(List<UniqueCaseRecord> uniqueCaseRecords) {
        this.uniqueCaseRecords = uniqueCaseRecords;
    }
}

Then we use this mapped superclass for a bunch of shared configuration and utility.

package mypackage.fully.qual;

@MappedSuperclass
public abstract class EntityBase {
    String guid;
    
    @Column(name = "GUID", nullable = false, length = 16)
    @Id
    @GeneratedValue(generator = "custom-uuid")
    @GenericGenerator(name = "custom-uuid", strategy = "mypackage.fully.qual.UUIDHexGenerator")
    @Type(com.ciminc.hibernate.type.GuidType.class)
    public String getGuid() {
        return this.guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    Integer version;
    @Version
    public Integer getVersion(){
        return this.version;
    }

    public void setVersion(Integer version){
        this.version = version;
    }
}

Which Hibernate version are you using? We actually have tests for these cases that ensure the collection is not initialized if the “owning side” is the many-to-one association.

We’re using Hibernate Core 6.1.6.

Update to 6.2.4.Final then please.