I have a hibernate project that connects to two data sources and has a base dao. I use the base dao to derive generic daos for the data sources as well.
I have a service that fetches a join relationship and I am running into the following error when ever an entity has a one to many relationship. I am stuck in that I am not able to get the one to many relationship objects.
I used the apache commons beanutils to copy properties hoping it would do a deep copy but it does not work either. I am running out of ideas.
I read that I there is the option to use Open Session In View but I am not sure how to go about that.
Error
failed to lazily initialize a collection of role: com.sony.spe.mc.domain.OperationalUnit.user, could not initialize proxy - no Session
I have the following layers and files
Controller Layer
@ApiOperation(value = "read", nickname = "getByID")
@RequestMapping(value="/read", method = RequestMethod.GET)
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "User's id", required = true, dataType = "int", paramType = "query")
})
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = User.class),
@ApiResponse(code = 500, message = "Failure")})
@ResponseBody
public UserProxy getByID(long id) {
UserProxy user;
try {
user = userService.fetchUserById(id);
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
return user;
}
The Service Layer
User Service
@Service
@Transactional("fuseTransactionManager")
public class UserServiceImpl extends BaseFuseServiceImpl<User> implements UserService {
@Autowired
UserDao userDao;
@Override
protected UserDao getDao() {
return userDao;
}
@Override
public UserProxy fetchUserById(long id) {
try {
/***** ERROR HAPPENS HERE. THE USER OBJECT IS NOT ABLE TO GET THE ONE TO MANY OBJECTS even though I have @Transactional *****/
User user = userDao.fetchEntityById(User.class, id);
UserProxy userProxy = new UserProxy();
BeanUtils.copyProperties(userProxy, user);
return userProxy;
} catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
.....
Generic Fuse Service
@Service
public class BaseFuseServiceImpl<T extends Serializable> extends BaseServiceImpl<T> implements BaseFuseService<T> {
@Autowired
BaseFuseDao<T> baseFuseDao;
@Override
protected BaseFuseDao<T> getDao() {
return baseFuseDao;
}
}
Very generic service that the specific services implement
@Service
@Transactional("baseTransactionManager")
public abstract class BaseServiceImpl<T extends Serializable> implements BaseService<T> {
protected abstract BaseDao<T> getDao();
@Override
public T fetchEntityById(Class<T> entityClass, long id) {
return getDao().fetchEntityById(entityClass, id);
}
User Entity. There is also a corresponding proxy object that is pretty much identical except it does not have the @Entity @Column annotations.
@Entity
@Table(name="USER")
//@AttributeOverride(name = "ID", column = @Column(name = "USER_ID", nullable = false))
public class User extends BaseEntity {
//public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5236507646361562577L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "USER_ID")
private long id;
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name="USER_OPERATIONALUNIT",
joinColumns = {@JoinColumn(name="USER_ID")},
inverseJoinColumns = {@JoinColumn(name="OPERATIONALUNIT_ID")}
)
private Set<OperationalUnit> operationalUnit;
.... getters and setters