-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Collection is empty when insert and read in same Transaction
PostPosted: Sun Dec 29, 2013 2:05 pm 
Newbie

Joined: Wed Oct 22, 2008 1:24 am
Posts: 2
I'm trying to create a Hibernate entity and then read a one-to-many Collection in the same method/transaction and its always returning empty for the get Collection call. I've tried to use an `fetch=FetchType.EAGER` instead of `fetch=FetchType.LAZY` and I've also tried a Hibernate `flush()` after the car insert and before the car read, but the one-to-many Collection is always empty. If I remove the get Collection call (see getCarList() below) and put it in another method, the Collection is not empty but I would like to keep the insert and read in the same method if possible. If I do a flush and refresh then the Collection is populated, but isn't that practice discouraged? Ideas?

Entity:

Code:
  @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);
     }


Service Implementation:

Code:
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 Implementation:
Code:
        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;
                }
            }

Library Versions:

hibernate-core-4.2.0.final
spring-core-3.2.2.RELEASE.jar

Spring Hibernate Config:
Code:
<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>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.