我试图创建一个Hibernate实体,然后在相同的方法/事务中读取一对多Collection,并且对于get Collection调用,它总是返回空。我尝试使用fetch=FetchType.EAGER
代替fetch=FetchType.LAZY
,并且在插入汽车之后且读取汽车之前也尝试了Hibernate flush()
,但是“一对多”集合始终为空。如果我删除get Collection调用(请参见下面的getCarList())并将其放在另一个方法中,则Collection不是空的,但我想保留插入并在可能的情况下以相同的方法读取。我也尝试调用Hibernate.initialize(this.carList)
,但仍然为空。有想法吗?
实体:
@Entity
@Table(name = "car")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "car_type", discriminatorType = DiscriminatorType.STRING)
public class Car {
private Car defaultCar;
private List<Car> carList = new ArrayList<Car>();
@ManyToOne
@JoinColumn(name = "DEFAULT_CAR_ID")
public Car getDefaultCar() {
return defaultCar;
}
public void setDefaultCar(Car defaultCar) {
this.defaultCar = defaultCar;
}
@OneToMany(mappedBy = "defaultCar", fetch=FetchType.LAZY)
public List<Car> getCarList() {
return carList;
}
public void setCarList(List<Car> carList) {
this.carList = carList;
}
@Transient
public void initializeCarList() {
Hibernate.initialize(this.carList);
}
服务实施:
public List<Car> saveAndGetDefaultCarList(){
Car car1 = new Car();
carDAO.create(car1);
Car car2 = new Car();
car2.setDefaultCar(car1);
carDAO.create(car2);
List<Car> defaultCarList = car1.getCarList(); //always returning empty List
}
DAO实施:
public class CarDAOImpl implements CarDAO {
private DaoFactory daoFactory;
@Override
public Copy create(Car car) {
GenericDao<Car> dao = daoFactory.getDao(Car.class);
return dao.create(car);
}
public void setDaoFactory(DaoFactory daoFactory) {
this.daoFactory = daoFactory;
}
public void setCarDao(CarDAO carDAO) {
this.carDAO = carDAO;
}
}
public class HibernateDaoFactory implements DaoFactory {
private SessionFactory sessionFactory;
private HibernateSessionConfigurer sessionConfigurer;
public <T> GenericDao<T> getDao(Class<T> clazz) {
HibernateDao<T> dao = new HibernateDao<T>(clazz);
dao.setSessionFactory(sessionFactory);
dao.setSessionConfigurer(sessionConfigurer);
return dao;
}
public QueryDao getQueryDao() {
HibernateQueryDao dao = new HibernateQueryDao();
dao.setSessionFactory(sessionFactory);
dao.setSessionConfigurer(sessionConfigurer);
return dao;
}
public void flush() {
sessionFactory.getCurrentSession().flush();
}
public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
}
public void setSessionConfigurer(HibernateSessionConfigurer cfg) {
this.sessionConfigurer = cfg;
}
}
库版本:
hibernate-core-4.2.0.final
spring-core-3.2.2.RELEASE.jar
Spring Hibernate Config:
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
最佳答案
您的car1与car2.setDefaultCar(car1)
之前是同一对象,因为car2.setDefaultCar(car1);
仅更新car2实例。您想要的是在将car2存储在数据库中之后更新car1状态(从数据库中获取更改)。
为此,您需要使用flush()会话(或提交)并在car1实例上调用refresh(car1)
方法-它将从数据库中重新创建。
但是,还有其他方法-首先将car2添加到car1集合中,然后仅持久保留car1-这将导致car2也将持久存在(设置cascade="save-update"
时)。