1

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

请在我的两张桌子下面找到:

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;

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

@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 以查找所有带有人名的地址,我不确定上述方法是否正确,因为我将返回地址和人名,但我上面的方法返回一个对象地址.:(

任何帮助都是最受欢迎的。再次感谢

4

1 回答 1

1

因此,您要做的第一件事是仔细检查您的 Person 类。您的地址类有 @Table(name="ADDRESS") 和 @Entity,但我没有看到 Person 类的这些。确保将 Person 类映射到 PERSON 表。

根据您所描述的,我认为您甚至不需要命名查询来做您想做的事情。看起来您想要选择数据库中的所有地址并获取与该地址关联的人的名字。您已通过 @ManyToOne 加入将您的 Address 映射到 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();
于 2013-01-29T13:23:30.330 回答