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]
Your query should be: "SELECT * FROM playeraccount a WHERE a.playerId =:uuid"
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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.