gpt4 book ai didi

java - 为什么我从 hibernate 查询中得到空结果以及映射问题?

转载 作者:太空宇宙 更新时间:2023-11-04 12:06:06 26 4
gpt4 key购买 nike

我正在使用 hibernate 和 wildfly 构建一个网络应用程序。下面是我的一些文件的内容:

Hibernate模型user.java:

package org.declercq.reportbuilderback.models;



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity (name="User")
@Table(name = "users")
public class User {


@Id @GeneratedValue
@Column(name="id")
private int id;

@Column(name="username")
private String userName;

@Column(name="password")
private String password;

@Column(name="email")
private String emailAddress;

public User(String userName, String password, String emailAddress) {
super();
this.userName = userName;
this.password = password;
this.emailAddress = emailAddress;
}

public User() {
super();
}

public int getId() {
return id;
}





}

我的 hibernate 实用程序类:

package org.declercq.reportbuilderback.dao;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
//import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
private static SessionFactory sessionFactory;



public static SessionFactory getSessionFactory() {
if(sessionFactory == null){
// loads configuration and mappings
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();

// builds a session factory from the service registry
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

}
return sessionFactory;
}
}

我使用的DAO方法:

public List<User> getAllUsers(){
System.out.println("0");
Session session=HibernateUtil.getSessionFactory().openSession();
System.out.println("0,5");
Transaction tx = null;
List<User> allUsers=null;
try{
tx = session.beginTransaction();
System.out.println("1");
allUsers = session.createQuery("FROM org.declercq.reportbuilderback.models.User").getResultList();


System.out.println("2");

tx.commit();
System.out.println("3");
return allUsers;
}
catch(HibernateException e){
if(tx != null){
tx.rollback();
System.out.println("4");
}
return allUsers;
}
finally{
session.close();
System.out.println("5");

}
}

此方法由 Web 服务调用:

package org.declercq.reportbuilderback.webservices;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.declercq.reportbuilderback.dao.UserDao;
import org.declercq.reportbuilderback.models.User;

@Path("/userwebservice")
public class UserWebService {

@Path("/users")
@GET
@Produces("application/json")
public List<User> listUsers(){
System.out.println("HERE");
List<User>allUsers=new UserDao().getAllUsers();
System.out.println("Now here");
System.out.println("Size: "+allUsers.size());
return allUsers;
}

}

我的 hibernate 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">removed</property>
<property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/reportbuilderwebservices</property>
<property name="hibernate.connection.username">removed</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.connection.pool_size">20</property>
<mapping class="org.declercq.reportbuilderback.models.User"/>
<mapping class="org.declercq.reportbuilderback.models.Permission"/>
<mapping class="org.declercq.reportbuilderback.models.Role"/>
<mapping class="org.declercq.reportbuilderback.models.Vulnerability"/>
<mapping class="org.declercq.reportbuilderback.models.Finding"/>
</session-factory>
</hibernate-configuration>

当我将其部署到 Wildfly 时,我在控制台中看到以下内容:

16:47:17,758 INFO  [org.jboss.modules] (main) JBoss Modules version 1.5.2.Final
16:47:18,100 INFO [org.jboss.msc] (main) JBoss MSC version 1.2.6.Final
16:47:18,239 INFO [org.jboss.as] (MSC service thread 1-7) WFLYSRV0049: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) starting
16:47:20,325 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) WFLYDS0004: Found org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war in deployment directory. To trigger deployment create a file called org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war.dodeploy
16:47:20,488 INFO [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0039: Creating http management service using socket-binding (management-http)
16:47:20,532 INFO [org.xnio] (MSC service thread 1-6) XNIO version 3.4.0.Final
16:47:20,568 INFO [org.xnio.nio] (MSC service thread 1-6) XNIO NIO Implementation Version 3.4.0.Final
16:47:20,657 INFO [org.jboss.remoting] (MSC service thread 1-6) JBoss Remoting version 4.0.21.Final
16:47:20,748 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 38) WFLYCLINF0001: Activating Infinispan subsystem.
16:47:20,782 INFO [org.wildfly.extension.io] (ServerService Thread Pool -- 37) WFLYIO001: Worker 'default' has auto-configured to 8 core threads with 64 task threads based on your 4 available processors
16:47:20,881 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 46) WFLYNAM0001: Activating Naming Subsystem
16:47:20,896 WARN [org.jboss.as.txn] (ServerService Thread Pool -- 54) WFLYTX0013: Node identifier property is set to the default value. Please make sure it is unique.
16:47:20,897 INFO [org.jboss.as.jsf] (ServerService Thread Pool -- 44) WFLYJSF0007: Activated the following JSF Implementations: [main]
16:47:20,916 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 33) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
16:47:20,929 INFO [org.jboss.as.security] (ServerService Thread Pool -- 53) WFLYSEC0002: Activating Security Subsystem
16:47:20,940 INFO [org.jboss.as.connector] (MSC service thread 1-6) WFLYJCA0009: Starting JCA Subsystem (WildFly/IronJacamar 1.3.4.Final)
16:47:20,945 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) WFLYJCA0018: Started Driver service with driver-name = h2
16:47:20,963 INFO [org.jboss.as.security] (MSC service thread 1-3) WFLYSEC0001: Current PicketBox version=4.9.6.Final
16:47:21,244 INFO [org.jboss.as.webservices] (ServerService Thread Pool -- 56) WFLYWS0002: Activating WebServices Extension
16:47:21,318 INFO [org.jboss.as.naming] (MSC service thread 1-2) WFLYNAM0003: Starting Naming Service
16:47:21,334 INFO [org.jboss.as.mail.extension] (MSC service thread 1-2) WFLYMAIL0001: Bound mail session [java:jboss/mail/Default]
16:47:21,381 INFO [org.wildfly.extension.undertow] (MSC service thread 1-7) WFLYUT0003: Undertow 1.4.0.Final starting
16:47:21,904 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 55) WFLYUT0014: Creating file handler for path '/home/wouter/wildfly-10.1.0.Final/welcome-content' with options [directory-listing: 'false', follow-symlink: 'false', case-sensitive: 'true', safe-symlink-paths: '[]']
16:47:22,028 INFO [org.jboss.as.ejb3] (MSC service thread 1-8) WFLYEJB0482: Strict pool mdb-strict-max-pool is using a max instance size of 16 (per class), which is derived from the number of CPUs on this host.
16:47:22,029 INFO [org.jboss.as.ejb3] (MSC service thread 1-6) WFLYEJB0481: Strict pool slsb-strict-max-pool is using a max instance size of 64 (per class), which is derived from thread worker pool sizing.
16:47:22,040 INFO [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0012: Started server default-server.
16:47:22,042 INFO [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0018: Host default-host starting
16:47:22,225 INFO [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080
16:47:22,744 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
16:47:23,033 WARN [org.jboss.as.domain.management.security] (MSC service thread 1-4) WFLYDM0111: Keystore /home/wouter/wildfly-10.1.0.Final/standalone/configuration/application.keystore not found, it will be auto generated on first use with a self signed certificate for host localhost
16:47:23,066 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-8) WFLYDS0013: Started FileSystemDeploymentService for directory /home/wouter/wildfly-10.1.0.Final/standalone/deployments
16:47:23,085 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) WFLYSRV0027: Starting deployment of "org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war" (runtime-name: "org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war")
16:47:23,635 INFO [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-6) ISPN000128: Infinispan version: Infinispan 'Chakra' 8.2.4.Final
16:47:23,683 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0006: Undertow HTTPS listener https listening on 127.0.0.1:8443
16:47:23,697 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 64) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,722 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 64) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,728 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 58) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,704 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 65) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,734 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 65) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:23,731 INFO [org.infinispan.configuration.cache.EvictionConfigurationBuilder] (ServerService Thread Pool -- 58) ISPN000152: Passivation configured without an eviction policy being selected. Only manually evicted entities will be passivated.
16:47:24,060 INFO [org.jboss.ws.common.management] (MSC service thread 1-5) JBWS022052: Starting JBossWS 5.1.5.Final (Apache CXF 3.1.6)
16:47:26,386 INFO [org.jboss.weld.deployer] (MSC service thread 1-7) WFLYWELD0003: Processing weld deployment org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war
16:47:26,897 INFO [org.hibernate.validator.internal.util.Version] (MSC service thread 1-7) HV000001: Hibernate Validator 5.2.4.Final
16:47:27,312 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) WFLYJCA0005: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 9.4)
16:47:27,364 INFO [org.jboss.weld.Version] (MSC service thread 1-5) WELD-000900: 2.3.5 (Final)
16:47:27,465 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) WFLYJCA0018: Started Driver service with driver-name = org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war_org.postgresql.Driver_9_4
16:47:30,321 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 65) RESTEASY002225: Deploying javax.ws.rs.core.Application: class org.declercq.reportbuilderback.webservices.ConfigApp
16:47:30,383 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 65) WFLYUT0021: Registered web context: /org.declercq.reportbuilderback-0.0.1-SNAPSHOT
16:47:30,419 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war" (runtime-name : "org.declercq.reportbuilderback-0.0.1-SNAPSHOT.war")
16:47:30,634 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
16:47:30,635 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
16:47:30,637 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 13428ms - Started 476 of 724 services (404 services are lazy, passive or on-demand)
16:51:38,771 INFO [stdout] (default task-3) HERE
16:51:38,774 INFO [stdout] (default task-3) 0
16:51:38,891 INFO [org.hibernate.Version] (default task-3) HHH000412: Hibernate Core {5.2.3.Final}
16:51:38,895 INFO [org.hibernate.cfg.Environment] (default task-3) HHH000206: hibernate.properties not found
16:51:38,898 INFO [org.hibernate.cfg.Environment] (default task-3) HHH000021: Bytecode provider name : javassist
16:51:39,501 INFO [org.hibernate.annotations.common.Version] (default task-3) HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
16:51:39,688 WARN [org.hibernate.orm.connections.pooling] (default task-3) HHH10001002: Using Hibernate built-in connection pool (not for production use!)
16:51:39,690 INFO [org.hibernate.orm.connections.pooling] (default task-3) HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://127.0.0.1:5432/reportbuilderwebservices]
16:51:39,691 INFO [org.hibernate.orm.connections.pooling] (default task-3) HHH10001001: Connection properties: {user=reportbuilderwebservices, password=****}
16:51:39,691 INFO [org.hibernate.orm.connections.pooling] (default task-3) HHH10001003: Autocommit mode: false
16:51:39,695 INFO [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (default task-3) HHH000115: Hibernate connection pool size: 20 (min=1)
16:51:39,803 INFO [org.hibernate.dialect.Dialect] (default task-3) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
16:51:40,086 INFO [org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl] (default task-3) HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
16:51:40,091 INFO [org.hibernate.type.BasicTypeRegistry] (default task-3) HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@60f81f2e
16:51:40,453 INFO [org.hibernate.tool.schema.internal.SchemaCreatorImpl] (default task-3) HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@4788a59c'
16:51:40,642 INFO [stdout] (default task-3) 0,5
16:51:40,649 INFO [stdout] (default task-3) 1
16:51:40,681 WARN [org.hibernate.hql.internal.QuerySplitter] (default task-3) HHH000183: no persistent classes found for query class: FROM org.declercq.reportbuilderback.models.User
16:51:40,684 INFO [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (default task-3) HHH000397: Using ASTQueryTranslatorFactory
16:51:40,692 INFO [stdout] (default task-3) 2
16:51:40,693 INFO [stdout] (default task-3) 3
16:51:40,695 INFO [stdout] (default task-3) 5
16:51:40,696 INFO [stdout] (default task-3) Now here
16:51:40,697 INFO [stdout] (default task-3) Size: 0

问题出在控制台输出的最后几行。正如您在我的代码(DAO 方法和 Web 服务代码)中看到的,我在中间放置了一些 system.out.println 语句来进行故障排除。所有调用都正在执行,没有抛出任何错误。然而,当我打印数据库查询结果列表的大小时,它是空的(大小0)。

我将一些数据放入该用户表中,并手动检查数据是否存在。然而我的代码没有检索它......

另外,为什么我会看到这个:

16:51:40,681 WARN  [org.hibernate.hql.internal.QuerySplitter] (default task-3) HHH000183: no persistent classes found for query class: FROM org.declercq.reportbuilderback.models.User

我的 hibernate 配置文件中有一个映射,我在模型类 User.java 中有注释,但似乎缺少了一些东西?如果我将查询替换为“来自用户”,则会收到一条错误消息,指出用户未映射,但它在我的配置文件中?我被迫在 HQL 查询中使用完整的包+类名?

有人可以帮我解释一下吗?

谢谢!

最佳答案

尝试

 allUsers = session.createQuery("SELECT u FROM User u").getResultList();

关于java - 为什么我从 hibernate 查询中得到空结果以及映射问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40346003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com