没有懒惰地初始化集合连接

时间:2016-09-28 08:40:40

标签: java spring hibernate join

用户模型:

@Entity
@Table(name = "users")
public class User implements Serializable {
    @Id
    @GeneratedValue
    private int id;

    public int getId() {
        return id;
    }

    private String name;

    public String getName() {
        return name;
    }

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

    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @JsonIgnore
    private String password;

    public String getPassword() {
        return password;
    }

    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column(name = "house_number")
    @JsonProperty(value = "house_number")
    private String houseNumber;

    public String getHouseNumber() {
        return houseNumber;
    }

    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }

    private String zipcode;

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    private String city;

    public String getCity() {
        return city;
    }

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

    private String country;

    public String getCountry() {
        return country;
    }

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

    @Enumerated(EnumType.ORDINAL)
    private Role role;

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    @OneToMany
    @JoinColumn(name = "customer_id")
    private List<Order> orders;

    public List<Order> getOrders() {
        return orders;
    }

    @OneToMany
    @JoinColumn(name = "customer_id")
    private List<Picture> pictures;

    public List<Picture> getPictures() {
        return pictures;
    }

    @OneToMany
    @JoinColumn(name = "photographer_id")
    private List<Picture> uploads;

    public List<Picture> getUploads() {
        return uploads;
    }

    public User(String name,
                String email,
                String password,
                String address,
                String houseNumber,
                String zipcode,
                String city,
                String country,
                Role role)
    {
        this.name = name;
        this.email = email;
        this.password = password;
        this.address = address;
        this.houseNumber = houseNumber;
        this.zipcode = zipcode;
        this.city = city;
        this.country = country;
        this.role = role;
    }

    public User() {
        // Empty constructor
    }
}

订单型号:

@Entity
@Table(name = "order")
public class Order implements Serializable {
    @Id
    @GeneratedValue
    private int id;

    public int getId() {
        return id;
    }

    @JsonIgnore
    @OneToOne
    @JoinColumn(name = "customer_id")
    private User customer;

    public User getCustomer() {
        return customer;
    }

    @Column(name = "order_date")
    private Date date;

    public Date getDate() {
        return date;
    }

    private String status;

    public String getStatus() {
        return status;
    }

    public Order() {
        // Empty constructor because hibernate wants one >.>
    }

    public Order(int id, Date date, String status) {
        this.id = id;
        this.date = date;
        this.status = status;
    }
}

在UserDAOImpl:

@Repository
@Transactional
public class UserDaoImpl implements UserDao {
    private final SessionFactory sessionFactory;

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    public UserDaoImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public User get(int id) {
        try (Session session = sessionFactory.openSession()) {
            return session.get(User.class, id);
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }

        return null;
    }

    @Override
    public List<User> list() {
        Query query;

        try (Session session = sessionFactory.openSession()) {
            query = session.createQuery("from User");

            List<User> list = query.list();

            return list;
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }

        return null;
    }

    @Override
    public void delete(User object) {
        try (Session session = sessionFactory.openSession()) {
            session.delete(object);
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }
    }

    @Override
    public void update(User object) {
        try (Session session = sessionFactory.openSession()) {
            session.update(object);
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }
    }
}

当我想要执行UserDao.get(1)时,我收到此错误:

无法写入HTTP消息:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: 
com.exstodigital.photofactory.model.User.orders, could not initialize proxy - no Session (through reference chain: 
com.exstodigital.photofactory.model.User["orders"]); nested exception is 
com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: 
com.exstodigital.photofactory.model.User.orders, could not initialize proxy - no Session (through reference chain: 
com.exstodigital.photofactory.model.User["orders"])

0 个答案:

没有答案