0

I'm getting org.hibernate.hql.internal.ast.QuerySyntaxException when trying to execute a query with EntityManager.

I'm using Hibernate standalone in a Java SE application. I'm not using XML configuration.

This is how I get my session and EntityManager (settings variable is a Map<String, String>):

StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
registryBuilder.applySettings(settings);
registry = registryBuilder.build();
MetadataSources sources = new MetadataSources(registry);
Metadata metadata = sources.getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();

Session session = sessionFactory.openSession();
EntityManager em = session.getEntityManagerFactory().createEntityManager();

This is my entity class:

@Entity
@Table(name = "playeraccount")
public class PlayerAccount {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "playerId")
    private String playerId;
    @Column(name = "balance")
    private Double balance;

    [getters, setters...]
}

The table SQL:

CREATE TABLE PlayerAccount (
    id BIGINT NOT NULL UNIQUE PRIMARY KEY,
    playerId VARCHAR(255) NOT NULL UNIQUE,
    balance DOUBLE NOT NULL DEFAULT 0.0
);

And finally the query:

TypedQuery<PlayerAccount> q = em.createQuery("SELECT a FROM PlayerAccount a WHERE a.playerId = :uuid", PlayerAccount.class);
q.setParameter("uuid", uuid);
PlayerAccount acc = q.getSingleResult();

Whole exception is:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: PlayerAccount is not mapped [SELECT a FROM PlayerAccount a WHERE a.playerId = :uuid]
Jake S.
  • 568
  • 6
  • 25

3 Answers3

0

Your query should be : "SELECT * FROM playeraccount a WHERE a.playerId = :uuid"

MSeth
  • 81
  • 7
  • Still the same exception. It just occurred to me that MySQL table names are not case sensitive on Windows. Could that have an effect? – Jake S. Dec 02 '20 at 11:32
  • https://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html#setup-requirements – MSeth Dec 02 '20 at 11:45
  • Follow the above link to use XML configuration file or programmatically to enable your Entity to be picked up by Hibernate for mapping. Also you should let hibernate create the tables for you, rather than running the SQL yourself to avoid any mismatch in the Entity definition and SQL Table. – MSeth Dec 02 '20 at 11:50
0
Configuration configuration = new Configuration();
EntityScanner.scanPackages("my.com.entities", "my.com.other.entities")
    .addTo(configuration);
SessionFactory sessionFactory = configuration.buildSessionFactory();

I missed the part about lack of xml :)

or

List<Class<?>> classes = EntityScanner
        .scanPackages("my.com.entities", "my.com.other.entities").result();

MetadataSources metadataSources = new MetadataSources();
for (Class<?> annotatedClass : classes) {
    metadataSources.addAnnotatedClass(annotatedClass);
}

SessionFactory sessionFactory = metadataSources.buildMetadata()
    .buildSessionFactory();

Can hibernate scan packages to create SessionFactory automatically?

JCompetence
  • 6,997
  • 3
  • 19
  • 26
0

Use the standard approach to create an EntityManagerFactory which is:

Persistence.createEntityManagerFactory("PersistenceUnitName", settings)

You can cast or unwrap this to a SessionFactory if you like. Also, make sure the persistence.xml contains the FQN to the entity class. You can also follow the following tutorial if you want to use the Hibernate native APIs: https://docs.jboss.org/hibernate/orm/current/quickstart/html_single/#tutorial_annotations

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58