Tôi đang sử dụng spring-data-jpa trên dự án webmvc mùa xuân. Tôi đang gặp sự cố khi sử dụng query creation trên một Kho lưu trữ của một trong các thực thể của tôi. Dưới đây bạn có thể thấy thực thể của tôi, Repository của tôi và ngoại lệ.Kho lưu trữ Spring-Data-Jpa - Gạch dưới tên cột thực thể
My Entity,
@Entity
@Table(schema = "mainschema")
@XmlRootElement
public class Municipalperson implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "municipal_id", nullable = false)
private Integer municipal_id;
@Basic(optional = false)
@Column(nullable = false, length = 60)
private String firstname;
public Municipalperson(Integer id, Integer municipal_id, String firstname) {
this.id = id;
this.municipal_id = municipal_id;
this.firstname = firstname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getMunicipal_id() {
return municipal_id;
}
public void setMunicipal_id(Integer municipal_id) {
this.municipal_id = municipal_id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
Repository của tôi,
@Repository
public interface MunicipalpersonRepository extends JpaRepository<Municipalperson, Integer> {
List<Municipalperson> findByMunicipal_idOrderByLastnameDesc(int municipal_id);
}
và ngoại lệ,
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'municipalpersonRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property municipal found for type Municipalperson!
Tôi cố gắng để thiết lập mynicipal_id như int, sau đó là Integer và tương tự cho tham số 'municipal_id' trên Repository của tôi, nhưng tất cả chúng đều không hoạt động. Ngoài ra, tôi đổi tên Repository thành 'findByMunicipalidOrderByLastnameDesc' và 'findByMunicipalIdOrderByLastnameDesc' nhưng nó không hoạt động.
Cuối cùng tôi đổi tên các municipal_id để municipalId (gạch bỏ) và cũng có thể đổi tên getters/setters và Repository (findByMunicipalIdOrderByLastnameDesc) và vấn đề giải quyết.
Câu hỏi của tôi là tại sao điều này xảy ra?
JPA dữ liệu Spring dựa nhiều vào quy ước đặt tên Java. Bạn đã chọn không tôn trọng họ bằng cách có dấu gạch dưới trong tên trường và tên người truy cập của bạn. Đó là lý do tại sao bạn bị cắn. Tôn trọng các quy ước đặt tên Java và JPA dữ liệu mùa xuân sẽ được hạnh phúc và mã của bạn sẽ dễ đọc hơn như một phần thưởng. Không phải là cột có thể được đặt tên theo ý muốn của bạn, bao gồm cả dấu gạch dưới. Điều quan trọng là tên trường được ánh xạ của bạn. –
Cảm ơn bạn rất nhiều vì đã bình luận. Tôi không biết về các quy ước đặt tên java. Tôi cũng tìm thấy [câu hỏi stackoverflow] này (http://stackoverflow.com/questions/150192/using-underscores-in-java-variables-and-method-names) đã giúp tôi. –