使用 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;
}
}
人.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);
}
}
提前致谢!