要遍历的节点不能为SQL SQL SELECT

时间:2015-09-25 18:54:38

标签: java arrays sql-server hibernate

我在HQL中遇到问题,我的SQL基础也有一些数据库,所以需要把一个查询放在底部,我的sql base总是在数据库'数据库'然后我必须参考数据库webproduction,所以我写了我的查询:

@Entity
@Table(name="File")
@NamedQueries(

        {
            @NamedQuery(name="file.allList",
                    query = "use webproduction select * from File")
        }
)

我的配置:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://10.11.1.05</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password"></property>

        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

        <!-- <mapping class="entity.Sell" /> -->
        <mapping class="entity.File" />

    </session-factory>
</hibernate-configuration>

我的班级休眠:

public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {

            sessionFactory = new AnnotationConfiguration().configure(
                    "config/sql_hibernate.cfg.xml").buildSessionFactory();
        } catch (Throwable ex) {

            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

我的方法:

public List<File> getFile() throws Exception{
        session = HibernateUtil.getSessionFactory().openSession();
        query = session.getNamedQuery("file.allList");
        List<File> list1 = query.list();

        session.close();

        return list1;
    }

1 个答案:

答案 0 :(得分:0)

对于SQL Server,您将从命名查询中删除“使用webproduction”并使用:

JDBC:SQLSERVER://10.11.1.05;数据库名= webproduction

作为你的hibernate.connection.url

请参阅http://www.java2s.com/Tutorial/Java/0340__Database/AListofJDBCDriversconnectionstringdrivername.htm