JPA - 人与地址之间的OneToMany关联但无法创建具有关联的@NamedQuery

时间:2013-01-29 12:19:33

标签: hibernate jpa spring-mvc hibernate-mapping

我是hibernate和jpa的新手。基本上我有2个表人和地址。一个人可以有很多地址。我和人和地址之间有一个OnetoMany关系。我想得到每个人的名字和地址。

请在下面的2个表中找到:

ADDRESS: ADDRESSID,ADDRESS,PERSONID

PERSON:ID, FIRTSNAME,LASTNAME,PASSWORD.

我不知道如何为下面相应的sql创建@NamedQuery的语法:

select PERSON.FIRSTNAME, ADDRESS.ADDRESS
FROM PERSON
INNER JOIN ADDRESS
ON PERSON.id = ADDRESS.PERSONID;

请找到地址实体类,我假设select语句将在下面的类中:

@Entity
@NamedQuery(name="getAddressWithPersonFirstName",query=" ")
@Table(name = "ADDRESS")
public class Address {

@Id
@Column(name = "ADDRESSID")
private int addressId;

@Column(name = "ADDRESS")
private String address;

@Column(name = "PERSONID")
private int personId;


@ManyToOne
@JoinColumn(name="personId")
private Person person;


public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}

public int getAddressId() {
    return addressId;
}

public void setAddressId(int addressId) {
    this.addressId = addressId;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public int getPersonId() {
    return personId;
}

public void setPersonId(int personId) {
    this.personId = personId;
}

}

我的第二个实体类:

public class Person implements Serializable {

private static final long serialVersionUID = -1308795024262635690L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column
private String firstName;

@Column
private String lastName;

@Column
private String password;
@OneToMany(targetEntity = Address.class, mappedBy = "person")
private List<Address> address;


public List<Address> getAddress() {
    return address;
}

public void setAddress(List<Address> address) {
    this.address = address;
}

public Person() {
}

public Person(String firstName, String lastName) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
}

public String getPassword() {
    return password;
}

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

public Long getId() {
    return id;
}

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

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Override
public String toString() {

    return super.toString() + " name = " + firstName + " " + lastName
            + " id = " + id;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((firstName == null) ? 0 : firstName.hashCode());
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result
            + ((lastName == null) ? 0 : lastName.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Person other = (Person) obj;
    if (firstName == null) {
        if (other.firstName != null)
            return false;
    } else if (!firstName.equals(other.firstName))
        return false;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    if (lastName == null) {
        if (other.lastName != null)
            return false;
    } else if (!lastName.equals(other.lastName))
        return false;
    return true;
}

}

请找到我将运行查询的方法:

    public List<Address> getAddressByPersonFirstName() {

    TypedQuery<Address> query = entityManager.createNamedQuery(
            "getAddressWithPersonFirstName", Address.class);
    List<Address> results = query.getResultList();
    return results;

}

如上所述,我的两个主要问题是如何创建@NamedQuery以查找具有人名的所有地址,我不确定上述方法是否正确,因为我将返回地址和人名但我的方法返回上面一个对象地址。:(

欢迎任何帮助。再次提醒

1 个答案:

答案 0 :(得分:1)

所以,你要做的第一件事就是仔细检查你的Person类。你的地址类有@Table(name =“ADDRESS”)和@Entity,但我没有看到Person类的这些。确保Person类映射到PERSON表。

根据您所描述的内容,我认为您甚至不需要命名查询来执行您想要执行的操作。看起来您想要选择数据库中的所有地址,并获取与该地址关联的人员的名字。您已使用@ManyToOne连接将地址映射到Person类。

所以第一步是获取所有地址。有几种方法可以尝试:

Query query = entityManager.createQuery("select a from Address a");
List<Address> addresses = (List<Address>) query.getResultList();

这将为您提供数据库中的每个地址。如果您希望名字与该地址一起使用,请从列表中提取单个地址:

address.getPerson().getFirstName();

现在,假设您想要为特定的名字创建一个命名查询。假设你想找到这个人的名字是“Bob”的所有地址。

@NamedQueries({
    @NamedQuery(name="getAddressWithPersonFirstName", query="select a from Address a inner join Person p 
            where p.firstName=:firstName")
})
@Entity
@Table(name = "ADDRESS")
public class Address {

然后调用命名查询:

Query query = entityManager.createNamedQuery("getAddressWithPersonFirstName")
query.setParameter(0, "Bob");
return (List<Address>) query.list();