मैं सिर्फ इस परियोजना के लिए पहला नाम और अंतिम नाम दिखाना चाहता हूं और मेरे पास यह इकाई वर्ग और भंडार है लेकिन त्रुटि है
@Entity
public class Customer {
private int id;
private String firstname;
private String lastname;
private String city;
private String country;
private String phone;
private Collection<Orders> ordersById;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "firstname")
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Basic
@Column(name = "lastname")
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Basic
@Column(name = "city")
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Basic
@Column(name = "country")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Basic
@Column(name = "phone")
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@OneToMany(mappedBy = "customerByCustomerId",fetch = FetchType.LAZY)
public Collection<Orders> getOrdersById() {
return ordersById;
}
public void setOrdersById(Collection<Orders> ordersById) {
this.ordersById = ordersById;
}
और मेरा भंडार है:
@Repository
public interface CustomerRepo extends JpaRepository<Customer, Integer> {
@Query(value = "SELECT firstname, lastname from Customer",nativeQuery = true)
List<Customer> findAllCustomer();
}
लेकिन मेरे पास इस क्वेरी का उपयोग करने के लिए यह त्रुटि है मैं एमएस डेटाबेस का उपयोग करता हूं SQL सर्वर त्रुटि है: SQL त्रुटि: 0, SQLState: S1093 कॉलम नाम आईडी मान्य नहीं है। सर्वलेट के लिए Servlet.service() पथ के संदर्भ में [dispatcherServlet] [] फेंक दिया अपवाद [अनुरोध प्रसंस्करण विफल; नेस्टेड अपवाद है org.springframework.orm.jpa.JpaSystemException: क्वेरी निष्पादित नहीं कर सका; नेस्टेड अपवाद है org.hibernate.exception.GenericJDBCException: क्वेरी निष्पादित नहीं कर सका] मूल कारण के साथ
Hibernate:
SELECT
firstname,
lastname
from
Customer
2020-09-25 23:13:23.634 WARN 7712 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: S1093
2020-09-25 23:13:23.634 ERROR 7712 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : The column name id is not valid.
2020-09-25 23:13:23.672 ERROR 7712 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query] with root cause
1 उत्तर
आपको मानचित्रण जोड़ने की आवश्यकता है, इसलिए इस वर्ग को जोड़ें
public class CustomerResult{
private String firstname;
private String lastname;
public CustomerResult(String surname, String lastname){
this.firstname = firstname;
this.lastname = lastname;
}
// getters / setters
}
और इसे अपने रिपॉजिटरी में जोड़ें
@Query("select NEW com.yourpackageClass.CustomerResult(
o.customer.firstname, o.customer.lastname)
from Customer as o")
List< CustomerResult> findAllCustomer();
आशा है उपयोगी
संबंधित सवाल
नए सवाल
spring
स्प्रिंग फ्रेमवर्क जावा प्लेटफॉर्म पर एप्लिकेशन डेवलपमेंट के लिए एक ओपन सोर्स फ्रेमवर्क है। इसके मूल में घटक आधारित आर्किटेक्चर के लिए समृद्ध समर्थन है, और इसमें वर्तमान में बीस से अधिक उच्च एकीकृत मॉड्यूल हैं।