java - 我想使用 Hibernate 查询语言执行存储过程调用,并尝试以 pe 的方式执行此操作

标签 java mysql hibernate

这是我用来调用 Customer 类的主要方法

 public class HQLMain {

    //  public static void usingHibernateQuery(){
    //      SessionFactory sf = HibernateUtil.sf;
    //      Session session = sf.openSession();
    //      //Query query =session.createQuery("from Customer");
    //      Query query =session.getNamedQuery("selectName");
    //      query.setString("name", "master");  
    //
    //      List<Customer> customers = query.list();
    //      customers.forEach(customer-> System.out.println(customer.getName()));
    //  }

        public static void usingStoredProcedure(){
            SessionFactory sf = HibernateUtil.sf;
            Session session = sf.openSession();
            //Query query =session.createQuery("from Customer");
            Query query =session.getNamedQuery("callCustomerStoredProc");
            query.setString("name", "master");  
            List<Customer> customers = query.list();
            customers.forEach(customer-> System.out.println(customer.getName()));
        }

        public static void main(String[] args) {
        //usingHibernateQuery();
            usingStoredProcedure();

        }

这是用作实体的类,并在此处命名为存储过程,请帮助使用 hibernate 执行此存储过程调用,我不知道要在 java 中使用存储过程。我想知道何时给出 name="callCustomerStoredProc"从主方法调用它不起作用,请让我知道

    package org.jnit.domain;

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;

    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.NamedNativeQueries;
    import javax.persistence.NamedNativeQuery;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.OneToMany;
    import javax.persistence.OneToOne;
    import javax.persistence.OrderColumn;
    import javax.persistence.Table;

    import org.hibernate.annotations.LazyCollection;
    import org.hibernate.annotations.LazyCollectionOption;
    //@NamedQueries(value={@NamedQuery(name="selectName",query="from Customer where name= :name")})
    @Entity
    @Table(name = "customer")
    @NamedNativeQueries(value={@NamedNativeQuery(name="callCustomerStoredProc",query="call fetchCustomers(:name)",resultClass=Customer.class)})
    public class Customer implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue
        private int customerId;
        private String name;
        private String street;
        private String city;
        private String state;
        private String country;
        private String zipCode;

        @OneToOne(cascade = CascadeType.ALL, mappedBy = "customer", optional = true)
        private PhoneInfo phoneinfo;

        @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
        @OrderColumn(name="idx")
        @LazyCollection(LazyCollectionOption.EXTRA)
        private List<Order> ordersplaced = new ArrayList<Order>();

        public List<Order> getOrdersplaced() {
            return ordersplaced;
        }

        public void setOrdersplaced(List<Order> ordersplaced) {
            this.ordersplaced = ordersplaced;
        }

        public PhoneInfo getPhoneInfo() {
            return phoneinfo;
        }

        public void setPhoneInfo(PhoneInfo phoneInfo) {
            this.phoneinfo = phoneInfo;
        }

        public int getCustomerId() {
            return customerId;
        }

        public void setCustomerId(int customerId) {
            this.customerId = customerId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getStreet() {
            return street;
        }

        public void setStreet(String street) {
            this.street = street;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getZipCode() {
            return zipCode;
        }

        public void setZipCode(String zipCode) {
            this.zipCode = zipCode;
        }

    }

我收到以下错误:

 Hibernate: call fetchCustomers(?)
    Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:2066)
        at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863)
        at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
        at org.hibernate.loader.Loader.doQuery(Loader.java:910)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
        at org.hibernate.loader.Loader.doList(Loader.java:2554)
        at org.hibernate.loader.Loader.doList(Loader.java:2540)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
        at org.hibernate.loader.Loader.list(Loader.java:2365)
        at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
        at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873)
        at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
        at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
        at org.jnit.main.HQLMain.usingStoredProcedure(HQLMain.java:31)
        at org.jnit.main.HQLMain.main(HQLMain.java:37)
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: PROCEDURE demo.fetchCustomers does not exist
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        at com.mysql.jdbc.Util.getInstance(Util.java:381)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2228)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
        ... 15 more

最佳答案

验证您创建连接对象的用户 ID 是否有权执行上述过程

关于java - 我想使用 Hibernate 查询语言执行存储过程调用,并尝试以 pe 的方式执行此操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39629722/

相关文章:

MySQL JOIN + 子查询查询优化

java - JPA 不坚持我的类(class)

JavaMail 传输大型二进制 MIME 消息(rfc3030)

java - 我怎样才能杀死android中的其他前台 Activity ?

python - 尝试将信息发送到 mysql 数据库时,是否有 python 函数会拆分行?

MySQL 的 JOIN 和 COUNT

java - 在独立应用程序中有效缓存 Hibernate 分离对象?

java.lang.IllegalArgumentException : Parameter with that name [xxx] did not exist

java - 如何在 Kivy 中将 android.bluetooth.socket 输入流转换为 python 字符串?

java - 使用 KeyBIndings 响应按钮