我无法在hibernate中获取绑定参数值

yjghlzjz  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(430)

我在使用postgres数据库,我有数据,我在查询中传递字符串值。我没有得到任何错误,但是没有检索到值为什么?我不知道是什么原因请帮帮我
userdata.class类

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class UserData {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)

    private int userId;

    private String userName;
public UserData() {}
    public UserData(String userName) {
        this.userName = userName;

    }

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "UserData [userId=" + userId + ", userName=" + userName + "]";
    }

休眠测试类

package org.Dhiya.hibernate;

import java.util.List;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.javabrains.thila.dto.UserData;
public class HibernateTestt {

    public static void main(String[] args) {
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
        Session session=sessionFactory.openSession();
        session.beginTransaction();

        String minUserId="5";
        String userName="User 10";

        Query query=session.createQuery("from UserData where userId > :userId and userName= :userName");
        query.setParameter("userId", Integer.parseInt(minUserId));
         query.setParameter("userName", userName);
       List<UserData> users= (List<UserData>)query.list();

            session.getTransaction().commit();
            session.close();

           for(UserData user:users)

                System.out.println(user.getUserName());
}
}

慰问

Dec 24, 2020 8:51:13 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 5.4.24.Final
Dec 24, 2020 8:51:13 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
Dec 24, 2020 8:51:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Dec 24, 2020 8:51:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/hibernatedb1]
Dec 24, 2020 8:51:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=postgres}
Dec 24, 2020 8:51:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Dec 24, 2020 8:51:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Dec 24, 2020 8:51:14 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Dec 24, 2020 8:51:15 AM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: org.javabrains.thila.dto.TwoWheeler
Dec 24, 2020 8:51:15 AM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: org.javabrains.thila.dto.FourWheeler
Dec 24, 2020 8:51:15 AM org.hibernate.resource.beans.spi.ManagedBeanRegistryInitiator resolveBeanContainer
INFO: HHH10005002: No explicit CDI BeanManager reference was passed to Hibernate, but CDI is available on the Hibernate ClassLoader.
Dec 24, 2020 8:51:15 AM org.hibernate.search.engine.Version <clinit>
INFO: HSEARCH000034: Hibernate Search 5.11.7.Final
Dec 24, 2020 8:51:15 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@3337d04c] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Dec 24, 2020 8:51:16 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: select userdata0_.userId as userid1_0_, userdata0_.userName as username2_0_ from UserData userdata0_ where userdata0_.userId>? and userdata0_.userName=?

我得到了上面的输出,我没有得到任何错误,但我也没有得到字符串值。我在等你的回复谢谢!

x6h2sr28

x6h2sr281#

这里有一些提示,
请检查表中是否存在给定输入的数据。
获取数据,然后将其强制转换为userdata对象
在关闭会话和提交之前打印for循环。

相关问题