HQLnate的HSQLDB产生空结果

时间:2016-07-26 16:34:38

标签: java spring hibernate hsqldb

我尝试保存\从我的数据库中获取一些数据,但没有结果。

服务器日志:Hibernate:选择user0_.ID作为ID1_0_,user0_.LOGIN作为LOGIN2_0_,user0_.PASSWORD作为PASSWORD3_0_来自USER user0 _

OR

Hibernate:drop table USER如果存在 Hibernate:创建表USER(默认情况下生成的ID整数为identity(以1开头),LOGIN varchar(255),PASSWORD varchar(255),主键(ID)) Hibernate:插入USER(ID,LOGIN,PASSWORD)值(默认值,?,?)

我不知道问题是什么。

我正在使用Jetty,内存数据库

hibernate.cfg.xml中

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.archive.autodetection">class,hbm</property>
        <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.username">SA</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.url">jdbc:hsqldb:mem:dvd</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <mapping class="secure.entity.User"></mapping>
    </session-factory>
</hibernate-configuration>

用户

package secure.entity;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "USER")
public class User implements Serializable {

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

    @Column(name = "LOGIN")
    private String login;

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

    public User() {

    }

    public long getId(){ return this.id; }

    public void setId(int id){ this.id = id; }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

的HibernateUtil

package secure.config;

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

public class HibernateUtil
{
    private static SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory()
    {
        try
        {
            if (sessionFactory == null)
            {
                Configuration configuration = new Configuration().configure(HibernateUtil.class.getResource("/hibernate.cfg.xml"));
                StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
                serviceRegistryBuilder.applySettings(configuration.getProperties());
                ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            }
            return sessionFactory;
        } catch (Throwable ex)
        {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    public static void shutdown()
    {
        getSessionFactory().close();
    }
}

UserService

package secure.service;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Service;
import secure.config.HibernateUtil;
import secure.entity.User;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Override
    public User getByLogin(String login) {

        User usr = new User();

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            //String hql = "from User where login = :login";
            Query query = session.createQuery("FROM User");//.setParameter("login", login);
            List usrList = query.list();
            transaction.commit();

        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }

        return usr;
    }


}

0 个答案:

没有答案