0

我创建了一个示例 Web 应用程序。我使用 MS SQL Server 2008 作为数据库并使用 hibernate + 注释来访问数据库。我的休眠配置xml如下。问题是,Criteria.list() 返回一个空列表,而且我看到一个“?” 在生成的 HSQL 中,而不是我在 Criteria 中传递的参数。

<session-factory name="">
    <property name="hibernate.connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="hibernate.connection.url">jdbc:odbc:dbname</property>

    <property name="connection.pool_size">10</property>
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>       

     <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>       
    <property name="hibernate.connection.username"></property>
    <property name="hibernate.connection.password"></property>

<mapping class="com.demo.Person" />
</session-factory>

这是我的注释豆

    @Entity
    @Table(name = "person")
    public class Person implements Serializable {

        public Person(){

        }
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO) 
        @Basic(optional = false)
        @Column(name = "personid")
        private Integer personid;

        @Basic(optional = false)
        @Column(name = "firstname")
        private String firstname;

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

        @Basic(optional = false)
        @Column(name = "phone")
        private String phone;

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

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

        @Basic(optional = false)
        @Column(name = "city")
        private String city;

        @Basic(optional = false)
        @Column(name = "country")
        private String country;

        @Basic(optional = false)
        @Column(name = "bussinessowner")
        private int bussinessowner;

        @OneToMany(cascade = CascadeType.ALL, mappedBy = "resultid1")
        private Collection<Recent> recentCollection;

//setters & getters
}

我正在运行的代码是

Session session;
        List list = new ArrayList();
        try{
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Person.class);
        criteria.add(Restrictions.like("firstname", name, MatchMode.START));
        list= criteria.list();
        System.out.println(list.size());

        }catch (Exception e) {
            e.printStackTrace();
        }

生成的 HSQL :

Hibernate: select this_.personid as personid2_0_,  this_.city as city2_0_, this_.country as country2_0_, this_.firstname as firstname2_0_, this_.lastname as lastname2_0_ from person this_ where this_.firstname like ?

除此之外,我也没有任何例外。你能帮我解决这个问题吗?谢谢 !

4

4 回答 4

0

而不是这条线

criteria.add(Restrictions.like("firstname", name, MatchMode.START)); 

尝试

criteria.add(Restrictions.like("firstname", name, MatchMode.ANYWHERE)); 

或者

criteria.add(Restrictions.ilike("firstname", name, MatchMode.ANYWHERE)); 

如果您将名字传递为“abcd” ,它将生成 SQL 作为名字,如 '%abcd%'

你能简单地试试这个吗

session = HibernateUtil.getSessionFactory().openSession(); 
List<Person> personList = session.createQuery("Select * from Person p where p.firstname like 'rake%'").list();

System.out.println("Person result :" + personList .size());
于 2012-10-12T09:36:37.737 回答
0

改变它有criteria.add(Restrictions.like("firstname", name + "%"));帮助吗?而且,顺便说一句,?是正确的参数占位符jbdc语句。

于 2012-10-12T09:22:55.317 回答
0

“?” 是 JDBC 语句中参数的占位符。你看不到它的实际价值,仅此而已。没有问题,只是使用休眠的跟踪仍然不完整。

如果您想查看 '?' 的实际值,则必须使用额外的产品,例如log4jdbc

于 2012-10-12T10:39:51.683 回答
0

@Rakesh 试试下面的代码:

criteria.add(Restrictions.ilike("firstname", name +"%"));
于 2012-10-12T09:58:53.347 回答