Hibernate和constraints主键抛出QueryException

时间:2013-07-22 17:23:32

标签: java oracle hibernate constraints

使用oracle DB,我们创建了一个没有PK的表(我们知道它不是一个好的做法,但我们仍然希望有人可以帮助我们),我们需要使用约束作为替代。我们能够将数据插入到数据库中,遗憾的是我们无法检索它,因为它会引发异常。

Exception in thread "main" org.hibernate.QueryException: could not resolve property: name of: hibernate.Person [FROM hibernate.Person WHERE name = :name AND nickname= :nickname]

冬眠映射

<hibernate-mapping>
    <class name="com.sample.Person" table="PERSONS" schema="person_schema">
        <composite-id name="id" >
            <key-property name="name" column="NAME"  />
            <key-property name="nickname" column="NICKNAME" />
        </composite-id>

        <property name="address" type="java.lang.String">
            <column name="ADDRESS" length="100" />
        </property>

    </class>
</hibernate-mapping>

PersonDao.java

package hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class PersonDAO extends BaseHibernateDAO {


    public List<Person> getAll(Person person) {

        try {
            Query query = getSession().createQuery("FROM PERSONS " +
                    "WHERE name = :name " +
                    "AND nickname = :nickname");

            query.setString("name", person.getName());
            query.setString("nickname", person.getNickname());

            List<Person> persons =(List)query.list();


            return persons;
        } catch (RuntimeException re) {
            System.out.println("get failed");
            throw re;
        }finally{
            getSession().close();
        }
    }

    public void save(Person person) {
        try {
            Transaction tx = getSession().beginTransaction();
            getSession().save(person);
            tx.commit();
        } ...       
    }



}

PersonId.java

package hibernate;

import java.io.Serializable;

public class PersonId implements Serializable {
    private String name;
    private String nickname;
    private String address;

    //  getters & setters .....

    // an easy initializing constructor
    public PersonId(String name, String nickname, String address){
        this.name = name;
        this.nickname = nickname;
        this.address = address;
    }

    @Override
    public boolean equals(Object arg0) {
        if(arg0 == null) return false;
        if(!(arg0 instanceof PersonId)) return false;
        PersonId arg1 = (PersonId) arg0;
        return (this.name.equalsIgnoreCase(arg1.getName()) 
                    && (this.nickname.equalsIgnoreCase(arg1.getNickname()))
                    && (this.address.equalsIgnoreCase(arg1.getAddress()))
                );

    }
    @Override
    public int hashCode() {
        int hsCode;
        hsCode = name.hashCode();
        hsCode = hsCode + nickname.hashCode();
        hsCode = 19 * hsCode + address.hashCode();
        return hsCode;
    }
}

Person.java

package hibernate;

public class Person {

    private PersonId id;
    private String name;
    private String nickname;
    private String address;

    //getters and setters ...
}

TestHibernate.java

package test;

import hibernate.Person;
import hibernate.PersonId;
import hibernate.PersonDAO;

import java.util.Iterator;

public abstract class TestHibernate {

    public static void main(String[] args) throws InterruptedException {

/*      
        Person p = new Person();
        p.setPersonId(new PersonId("Foor", "Foo", "Sample Address"));
        p.setName("Foor");
        p.setNickname("Foo");
        p.setAddress("Sample Address");
        p.setValue("0");
        PersonDao dao = new PersonDao();
        //dao.save(p);
*/      
        Person p = new Person();
        p.setName("Foor");
        p.setNickname("Foo");

        PersonDao dao = new PersonDao();
        dao.getAll(p);


    }
}

提前致谢!

2 个答案:

答案 0 :(得分:1)

查询开头缺少SELECT

调用createQuery()时,必须使用JPQL / HQL。因此,要么修改查询以使其成为HQL(使用实体类名称等等),要么使用createSQLQuery()代替。

答案 1 :(得分:0)

我可以在这里看到一些问题(我可能是错的,如果我这样做,请纠正我) -

 Query query = getSession().createQuery("FROM PERSONS " +
                    "WHERE name = :name " +
                    "AND nickname = :nickname");

因为你正在使用HQL, PERSON应为Person ,属性Class name。

Check this link获得解释。

由于