-->
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: object returning null unless the server is restarted.
PostPosted: Mon Dec 15, 2014 8:27 pm 
Newbie

Joined: Wed Dec 03, 2014 7:28 pm
Posts: 2
I am having an issue where my MissionAsset object returns null unless I restart the server. Here are the steps to recreate the issue.
a Mission contains Set<MissionAsset> that contains Asset. where Asset is null
    start the server
    view the saved missions via the getMissions method (all data is retrieved and returned correctly)
    create/save a new mission via the save method
    select the new mission via the getMissionByID method (Assets are null)
    close browser, reopen browser, select the new mission via the getMissions method (Assets are null)
    restart the server
    select the new mission via the getMissions method (all data is retrieved and returned correctly)

Here is my DAO

Code:

    /**
     * http://static.springsource.org/spring/docs/2.0.x/reference/orm.html
     * Inject the shared entityManager which is thread safe using the persistence unit.
     * @param entityManager
     */
    @PersistenceContext(type=PersistenceContextType.EXTENDED)
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    /**
     * {@inheritDoc}
     * @param mission
     * @return mission
     */
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public Mission save(Mission mission) {
        Mission missionx = null;
        try {
            missionx = entityManager.merge(mission);
        } catch (HibernateException e) {
            LOGGER.log(Level.ERROR, "Error creating the mission.", e);
        } catch(Exception e){
             LOGGER.log(Level.ERROR, "Error creating the mission.", e);
        }
        return missionx;
    }

  /**
     * {@inheritDoc}
     * @param missionId
     * @return {@link Mission}
     */
    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public Mission findByMisionId(Integer missionId, Boolean collections) {

        Mission mission = null;

        try {
            Session session = (Session) entityManager.getDelegate();
            if(collections) {

                mission = (Mission) session.createCriteria(Mission.class)
                        .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                        .setFetchMode("missionAsset", FetchMode.JOIN)     
                        .add(Restrictions.eq("missionId", missionId)).uniqueResult();
                if(mission != null) {
                    if((mission.getMissionPkg() != null) || (!mission.getMissionPkg().isEmpty())) {
                        Hibernate.initialize(mission.getMissionPkg());
                    }
                }
            } else {
                mission = (Mission) session.createCriteria(Mission.class)
                        .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                        .add(Restrictions.eq("missionId", missionId));
            }
        } catch (HibernateException e) {
            LOGGER.log(Level.ERROR, "Error getting the mission with mission ID " + missionId, e);
        }
        return mission;
    }

  /**
     * {@inheritDoc}
     * @return list of {@link Mission} objects.
     */
    @Override
    @SuppressWarnings("unchecked")
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public List<Mission> getMissions(Boolean collections) {
        List<Mission> missions = new ArrayList<Mission>();
        try {
            Session session = (Session) entityManager.getDelegate();
            if(collections) {
                missions = session.createCriteria(Mission.class)
                        .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                        .setFetchMode("missionAsset", FetchMode.JOIN)
                        .addOrder(Order.asc("missionId"))
                        .list();
                if(!missions.isEmpty()) {
                    for(Mission mission : missions) {
                        if((mission.getMissionPkg() != null) || (!mission.getMissionPkg().isEmpty())) {
                            Hibernate.initialize(mission.getMissionPkg());
                        }
                    }
                }
            } else {
                missions = session.createCriteria(Mission.class)
                        .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                        .addOrder(Order.asc("missionId"))
                        .list();
            }
        } catch(HibernateException e) {
            LOGGER.log(Level.ERROR, "Error getting the missions.", e);
        }
        return missions;
    }


The POJO class to return an Set<MissionAsset> that contains an Asset object
Code:
    /**
     * Getter method for a set of {@link MissionAsset}.
     * @return missionAsset
     */
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "missionassetassoc", catalog="missionimpacts",
    joinColumns = { @JoinColumn(name = "missionid", nullable=false, updatable=false) },
    inverseJoinColumns = { @JoinColumn(name = "id", nullable=false, updatable=false) })
    public Set<MissionAsset> getMissionAsset() {
        return missionAsset;
    }
   
    /**
     * Setter method for a set of {@link MissionAsset}.
     * @param missionAsset
     */
    public void setMissionAsset(Set<MissionAsset> missionAsset) {
        this.missionAsset = missionAsset;
    }


The POJO class to return the Asset
Code:
    @OneToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    @JoinColumns({
        @JoinColumn(name = "assetid", referencedColumnName = "assetid", insertable=false, nullable=false, updatable=false)
    })
    public Asset getAsset() {
        return asset;
    }

    /**
     * private so that the API cannot add to the nrdb.jmeasset table.
     * @param asset
     */
    @SuppressWarnings("unused")
    private void setAsset(Asset asset) {
        this.asset = asset;
    } 


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.