当我执行如下查询(1)时:
private EntityManagerFactory emf;
private EntityManager em;
emf=Persistence.createEntityManagerFactory("hibernate");
em=emf.createEntityManager();
Query query = em.createQuery("SELECT COUNT(*) FROM Album");
Long resul= (long) query.getSingleResult();
System.out.println("Media de precios:" + resul);
在这里,我的实体是:
package Modelo;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author javier
*/
@Entity
@Table (name="album")
public class Album implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="numalbum")
private int id;
@Column(name="titulo")
private String titulo;
@Column(name="autor")
private String autor;
@Column(name="precio")
private int precio;
public Album() {
}
public Album(String titulo, String autor) {
this.titulo = titulo;
this.autor = autor;
}
public Album(int id, String titulo, String autor, int precio) {
this.id = id;
this.titulo = titulo;
this.autor = autor;
this.precio = precio;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
public int getPrecio() {
return precio;
}
public void setPrecio(int precio) {
this.precio = precio;
}
@Override
public int hashCode() {
int hash = 5;
hash = 67 * hash + this.id;
hash = 67 * hash + Objects.hashCode(this.titulo);
hash = 67 * hash + Objects.hashCode(this.autor);
hash = 67 * hash + this.precio;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Album other = (Album) obj;
if (this.id != other.id) {
return false;
}
if (this.precio != other.precio) {
return false;
}
if (!Objects.equals(this.titulo, other.titulo)) {
return false;
}
if (!Objects.equals(this.autor, other.autor)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Album{" + "id=" + id + ", titulo=" + titulo + ", autor=" + autor + ", precio=" + precio + '}';
}
sql:
CREATE DATABASE IF NOT EXISTS `musica` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `musica`;
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `album`
--
CREATE TABLE `album` (
`numalbum` int(11) NOT NULL,
`titulo` varchar(30) NOT NULL,
`autor` varchar(30) DEFAULT NULL,
`precio` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Volcado de datos para la tabla `album`
--
INSERT INTO `album` (`numalbum`, `titulo`, `autor`, `precio`) VALUES
(8, 'motomami', 'rosalia', 80),
(20, 'bailar', 'sergio', 120);
持久化单元:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="hibernate" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/musica"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
当我执行代码(1)时,Apache Netbeans 12 的输出给出以下错误:
simpleorg.hibernate.query.sqm.InterpretationException:解释查询时出错 [SELECT COUNT(*) FROM Album];这可能表明语义(用户查询)问题或解析器中的错误
我不知道如何解决这个错误,所以我查看了hibernate 6.0手册。
提前致谢。
我尝试了几个选项,但没有一个是有效的。 我希望这个程序能显示数据库的行数。
您似乎正在尝试执行查询来获取
Album
表中的行数,但查询语法可能是问题所在。该错误表明查询解释存在问题。
尝试在查询中使用实体名称而不是表名称。这是代码的修改版本:
Query query = em.createQuery("SELECT COUNT(a) FROM Album a");
Long result = (Long) query.getSingleResult();
System.out.println("Number of rows in Album table: " + result);
此查询使用实体别名 (
a
),并且应该与 Hibernate 一起使用。尝试一下,看看是否能解决问题。