Exceptions in Hibernate development: could not initialize proxy - no Session

Keywords: Java Session Fragment Attribute

I. Abnormal Contents

could not initialize proxy - no Session

2. Analysis of Abnormal Reasons

First, I have the following fragment in my persistence class User:

//User.java
@Entity
@Table(name = "user_inf")
public class User {
    //User's Order Information
    @OneToMany(targetEntity=Order.class,mappedBy="user")
    private Set<Order> orders=new HashSet<Order>();
    //Eliminate other elements
}

The controller code snippet is as follows:

//GoodAction.java
/**
     * Submit orders, only users can operate
     * @return
     */
    public String submitOrder(){
        //Omit part of the code
        User user=userService.find(tel, password);
        //Privilege validation passes, submission order obtains submission status
        boolean result=goodService.submit((User)object,id);
        if(result) return SUCCESS;
        return ERROR;
    }

The user's business logic component code snippet is as follows:

//UserServiceImpl.java
@Override
    public User find(String tel, String password) {
        List<User> list=userDao.find("select u from User u left join fetch u.orders where u.tel=? and u.password=?",tel,password);
        return list.size()==0?null:list.get(0);
    }

The code snippet of the commodity business logic component is as follows:

//GoodServiceImpl.java
@Override
    public boolean submit(User user, Serializable goodId) {
        //Omit some irrelevant things
        //Downlink code can go wrong
        user.getOrders().add(order);
        return true;
    }

The main culprits are the following lines of code:

//The system reports errors in this line of code
user.getOrders().add(order);

The real error location is the following hql statement location:

//UserServiceImpl.java
@Override
    public User find(String tel, String password) {
        List<User> list=userDao.find("select u from User u left join fetch u.orders where u.tel=? and u.password=?",tel,password);
        return list.size()==0?null:list.get(0);
    }

The system gives us a hint: the current session is closed, and its orders attribute is not obtained when loading user object, because Hibernate defaults to use the way of delayed loading for the processing of persistent collection, and it does not specify that delayed loading is not used in the query, so an exception will occur.

3. Abnormal Solution

//UserServiceImpl.java
@Override
    public User find(String tel, String password) {
        List<User> list=userDao.find("select u from User u left join fetch u.orders where u.tel=? and u.password=?",tel,password);
        return list.size()==0?null:list.get(0);
    }

It is known that a sentence has been added to the hql statement:

left join fetch u.orders

It means that when querying User entities, the values of orders are also found and assigned to User entities.

Of course, it can also be solved by eliminating lazy loading.

Posted by space1 on Sun, 10 Feb 2019 03:00:18 -0800