我是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 以查找所有带有人名的地址,我不确定上述方法是否正确,因为我将返回地址和人名,但我上面的方法返回一个对象地址.:(
任何帮助都是最受欢迎的。再次感谢