我正在尝试在Spring MVC中使用JPA动态实体图。当我使用fetchgraph策略时,它会抛出
“ java.lang.illegalArgumentException”,显示消息“无法找到 在此具有给定名称[ecosystems]的属性 ManagedType [com.ith.betta.web.models.BaseModel]”。
但是当我选择loadgraph时,它工作正常。以下是我的实体
Order.java
package com.ith.betta.web.models;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "orders")
public class Order extends BaseModel {
private String orderNumber;
private String type;
private Integer quantity;
@ManyToOne
private User user;
@OneToMany(mappedBy = "order")
private List<Invoice> invoices;
public Order() {
}
public Order(String orderNumber, String type, Integer quantity) {
this.orderNumber = orderNumber;
this.type = type;
this.quantity = quantity;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}
Invoice.java
package com.ith.betta.web.models;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "invoices")
public class Invoice extends BaseModel {
@ManyToOne
private Order order;
}
User.java
package com.ith.betta.web.models;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
@Table(name = "users")
public class User extends BaseModel {
@NotNull
@Column(unique = true)
private String email;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
private String password;
private String username;
private Boolean enabled;
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private UserProfile userProfile;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Role> roles = new HashSet<Role>();
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Notification> notifications = new ArrayList<Notification>();
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Order> orders;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Ecosystem> ecosystems;
public User() {
}
public User(String email, String firstName, String lastName, String password, String username) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public UserProfile getUserProfile() {
return userProfile;
}
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public List<Notification> getNotifications() {
return notifications;
}
public void setNotifications(List<Notification> notifications) {
this.notifications = notifications;
}
@PrePersist
private void initializeCreatedAndUpdatedAt() {
LocalDateTime now = LocalDateTime.now();
this.createdAt = this.updatedAt = now;
}
@PreUpdate
private void updateUpdatedAt() {
this.updatedAt = LocalDateTime.now();
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
public List<Ecosystem> getEcosystems() {
return ecosystems;
}
public void setEcosystems(List<Ecosystem> ecosystems) {
this.ecosystems = ecosystems;
}
}
BaseModel.java
package com.ith.betta.web.models;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.*;
import java.time.LocalDateTime;
@MappedSuperclass
public class BaseModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
@CreationTimestamp
@Column(name = "created_at")
protected LocalDateTime createdAt;
@UpdateTimestamp
@Column(name = "updated_at")
protected LocalDateTime updatedAt;
public Long getId() {
return id;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
public LocalDateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}
}
最后是服务类
package com.ith.betta.web.services.impl;
import com.ith.betta.web.models.Order;
import com.ith.betta.web.services.OrderService;
import org.springframework.stereotype.Service;
import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@Service("orderService")
public class OrderServiceImpl implements OrderService {
@PersistenceContext
private EntityManager em;
@Override
public Optional<Order> findOrderWithInvoices(Long orderId) {
EntityGraph graph = em.createEntityGraph(Order.class);
graph.addSubgraph("invoices");
Map hints = new HashMap();
hints.put("javax.persistence.fetchgraph", graph);
Order order = em.find(Order.class, orderId, hints);
return Optional.of(order);
}
}
现在在上述设置用户关系中正在产生问题。当我使用loadgraph时,它也可以正常工作并加载用户,但是没有声明急切的加载。但是,当我切换到fetchgraph时,它会尝试找到用户->生态系统,由于某种原因,它找不到它并将其冒泡为基本模型。
正在使用的相关版本
修改1: 当我将ManyToOne用户关联的访存类型更改为“懒惰”(默认为“渴望”)时,它开始工作,不知道原因
编辑2 进一步深入研究,我在电子邮件列上删除了唯一的注释,现在它开始适用于Lazy和Eager提取类型。还是不知道为什么