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;
}