我正在尝试创建一个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配置:
<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>
答案 0 :(得分:1)
您的car1与car2.setDefaultCar(car1)
之前的对象相同,因为car2.setDefaultCar(car1);
仅更新car2实例。你想要的是,在将car2存储在数据库中之后更新car1状态(从数据库中获取更改)。
要在一个方法中执行此操作,您需要在car1实例上flush()会话(或提交)并调用refresh(car1)
方法 - 它将从数据库重新创建。
但是,还有其他方法 - 首先将car2添加到car1集合,然后只保留car1 - 这将导致car2也将被持久化(设置cascade="save-update"
时)。
答案 1 :(得分:0)
可能是因为你从一对多的方面反转了关系,但是没有将关系所有者定义为多对一方。
你反过来了。因此,一对多方面并不拥有这种关系。所有者将是属性&#34; defaultCar&#34;
@OneToMany(mappedBy = "defaultCar", fetch=FetchType.LAZY)
public List<Car> getCarList() {
return carList;
}
但是你还没有用ManyToOne
注释defaultCar尝试正确定义关系的所有者