惰性初始化在休眠中不起作用。这是课程
@Entity
@Table(name = "PERSONS")
public class Persons {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "person", cascade = CascadeType.REMOVE)
public List<Cards> cards = new ArrayList<Cards>();
public Persons() {
}
//getters and setters
}
卡片类
@Entity
@Table(name = "CARDS")
public class Cards {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "card", cascade = CascadeType.REMOVE)
@JsonManagedReference
public List<BalanceHist> balanceHists = new ArrayList<BalanceHist>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "card", cascade = CascadeType.REMOVE)
@JsonManagedReference
public List<Events> events = new ArrayList<Events>();
//getters and setters
}
正如您从图片中看到的那样,该方法有效,但我应该得到一个错误,因为如果您查看在 DAO 中获取用户的方法:
@Repository
@Transactional
public class PersonRepository extends AbstractRepository<Persons> {
public PersonRepository() {
super(Persons.class);
}
@Override
public List<Persons> getAll() {
return super.getAll();
}
}
DAO 的父类:
@Repository
@Transactional
public abstract class AbstractRepository<Entity> {
private Class persistentClass;
public AbstractRepository(Class persistentClass) {
this.persistentClass = persistentClass;
}
@Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public List<Entity> getAll() {
return getSession().createCriteria(persistentClass).list();
}
}
也就是说,我没有在任何地方初始化标有注释的实体字段@OneToMany(fetch = FetchType.LAZY, mappedBy = "card", cascade = CascadeType.REMOVE),并且根据想法,我应该在访问它们时收到错误,但由于某种原因,我立即从数据库中获取了所有依赖项。告诉我可能是什么问题。
@Repository
@Transactional
public class PersonRepository extends AbstractRepository<Persons> {
public PersonRepository() {
super(Persons.class);
}
@Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
@Override
public List<Persons> getAll() {
// Criteria criteria = createEntityCriteria();
// criteria.addOrder(Order.asc("personId"));
// return criteria.list();
List<Persons> personsList = getSession().createCriteria(Persons.class).list();
for (Persons persons : personsList) {
Hibernate.initialize(persons.getCards());
}
return personsList;
}
}
@Service
@Transactional
public class PersonService {
public List<Persons> getUsers() {
List<Persons> personList = new ArrayList<>();
for (Persons person : personRepository.getAll()) {
if (person.getRole().getRoleType().equals("USER"))
personList.add(person);
}
return personList;
}
}
@Controller
@RequestMapping("/")
public class AppController {
@RequestMapping(value = {"/", "/home"}, method = RequestMethod.GET)
public String homePage(ModelMap model) {
model.addAttribute("greeting", "Welcome to the first page of the project");
System.out.println("ASASAS");
for (Persons person: personService.getUsers()) {
for (Cards card:person.getCards()) {
System.out.println("TROLOLO");
System.out.println(card.getCardName());
if (Const.DEBUG) {
if (logger.isDebugEnabled()) {
logger.debug(card.getCardName());
}
}
}
}
return "welcome";
}
}

您自己一次以两种方式卸载惰性集合
PersonRepository#getAll:@Transactional,因此所有方法也是如此。因此,即使只是调用person.getCards()集合也会卸载。Hibernate#initialize如果事务处于活动状态,则卸载惰性集合,这就是您所做的。如果要捕获
LazyInitializationException,请不要在方法循环中卸载集合PersonRepository#getAll。