java如果类中没有此类字段,如何接收id上的可嵌入对象?
我是spring data jpa的新手。 类Person包含字段集<;'地址>;地址。当我将具有相关地址的对象person保存到数据库时,我对person\u id:joinColumns=@JoinColumn(name=“person\u id”)上的地址使用join。类地址没有字段id或人员id。数据保存正确。如何在AddressRepository中编写方法,使用person_id从db获取地址
班长:
@Entity
@NamedQuery(name = "Person.findByPhone",
query = "select p from Person as p join p.phoneNumbers as pn where pn.number = :number")
@Table(uniqueConstraints={
@UniqueConstraint(columnNames = {"lastName", "firstName", "patronymic"})
})
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String lastName;
@NotNull
private String firstName;
private String patronymic;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "person_phone_numbers", joinColumns = @JoinColumn(name = "person_id"))
private Set<PhoneNumber> phoneNumbers = new HashSet<>();
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "person_addresses", joinColumns = @JoinColumn(name = "person_id"))
@AttributeOverrides({
@AttributeOverride(name = "addressLine1", column = @Column(name = "house_number")),
@AttributeOverride(name = "addressLine2", column = @Column(name = "street"))
})
private Set<Address> addresses = new HashSet<>();
public Person() {
}
public Person(String lastName, String firstName, String patronymic,
Set<PhoneNumber> phoneNumbers, Set<Address> addresses) {
this.lastName = lastName;
this.firstName = firstName;
this.patronymic = patronymic;
this.phoneNumbers = phoneNumbers;
this.addresses = addresses;
}
getters and setters
}
班级地址
@Embeddable
public class Address {
@NotNull
@Size(max = 100)
private String addressLine1;
@NotNull
@Size(max = 100)
private String addressLine2;
@NotNull
@Size(max = 100)
private String city;
@NotNull
@Size
private String region;
@NotNull
@Size
private String country;
@NotNull
@Size
private String zipCode;
public Address() {
}
public Address(@NotNull @Size(max = 100) String addressLine1, @NotNull @Size(max = 100) String addressLine2,
@NotNull @Size(max = 100) String city, @NotNull @Size String region,
@NotNull @Size String country, @NotNull @Size String zipCode) {
this.addressLine1 = addressLine1;
this.addressLine2 = addressLine2;
this.city = city;
this.region = region;
this.country = country;
this.zipCode = zipCode;
}
getters and setters
}
接口地址存储库(它给我一个错误org.springframework.beans.factory.BeanCreationException:创建名为'requestMappingHandlerAdapter'的bean时出错):
public interface AddressRepository extends JpaRepository<Address, Long> {
Address findByPersonId(Integer person_id);
}
共 (0) 个答案