Spring Hibernate Saving Entities in Test Not Persisting - java

#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {HibernateConfigTest.class})
#Transactional
#Sql(scripts = {"api_routes.sql",
"profile.sql",
"status.sql",
"user.sql",
"game_token.sql",
"game.sql",
"message.sql"},
config = #SqlConfig(transactionMode = ISOLATED),
executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
#Sql(scripts = "delete_data.sql",
executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public class GameDaoTest {
#Autowired
private GameDao gameDao;
#Test
public void getGetRecentGames() {
Game game = null;
for (int i = 0; i < 1000; i++) {
game = new Game(i + 1000);
game.setStartedAt(DateUtils.getCurrentUTCDate());
gameDao.save("game", game);
}
List<Game> recentGames = gameDao.getRecentGames(1000);
assertNotNull(recentGames);
assertEquals(1000, recentGames.size());
}
}
When I get to the line List<Game> recentGames = gameDao.getRecentGames(1000); hibernate prints out all of the insert statements. Unfortunately, when the games are retrieved, none of the ones I inserted are retrieved. Is there any way to retrieve those games? Maybe a better question would be how do I put those inserts in their own transaction so they are persisted for the subsequent methods?
Here is the AbstractDao that GameDao extends:
public class AbstractDao {
#PersistenceContext
protected EntityManager entityManager;
protected Session getSession() {
return entityManager.unwrap(Session.class);
}
public void save(Object entity) {
getSession().save(entity);
}
public void save(String entityName, Object entity) {
getSession().save(entityName, entity);
}
public void persist(Object entity) {
getSession().persist(entity);
}
}
The persist method throws an exception for some detached entity reason that I am not familiar with.

Try calling flush on your session before calling getRecentGames. If that method is executing a custom query, as opposed to using one of Hibernate's session methods, it's going directly against the database -- but since your save hasn't yet been synced with the underlying database, the data isn't there. You don't actually need to do a true commit as long as Hibernate can see the data.
for (int i = 0; i < 1000; i++) {
game = new Game(i + 1000);
game.setStartedAt(DateUtils.getCurrentUTCDate());
gameDao.save("game", game);
}
gameDao.flush(); // calls getSession().flush()
List<Game> recentGames = gameDao.getRecentGames(1000);
// call assert methods as needed

Related

Testing persistence methods - JUnit

I am writing unit tests for my project, but facing some difficulties when calling methods, which work with database. Currently I want to check a method that gets the list of publications that are of users interest, but I am getting NullPointerException:
public class TestPubManager {
private PubManager pFunc;
#Before
public void initialize() {
EntityManager manager = PersistenceManager.INSTANCE.getEntityManager();
em.getTransaction().begin();
PubManager pManager = new PubManager(manager);
}
#Test
public void testGetInterestPubs() {
int res = pManager.getInterestPubs(2).size();
assertEquals(20, res);
}
}
NullPointerException is on the line with int res = pManager.getInterestPubs(2).size();. What am I doing wrong way?
I have found a solution. So the issue was in the constructor - it was not initializing in #Before annotation, but when I put it inside the test, everything worked out fine.
public class TestPubManager {
private PubManager pFunc;
EntityManager manager = PersistenceManager.INSTANCE.getEntityManager();
#Before
public void initialize() {
em.getTransaction().begin();
}
#Test
public void testGetInterestPubs() {
PubManager pManager = new PubManager(manager);
int res = pManager.getInterestPubs(2).size();
assertEquals(20, res);
}
}

Too Many Connections Error: Hibernate

I am using Hibernate in my web application which is developed with pure Servlet and JSP. I am facing to a big trouble "sometimes" when I execute the code. What happens is I am getting Too many Connections error from Hibernate.
I went through lot of Stackoverflow questions seeking for an answer and I found different solutions. Some suggested to use a third party pooling system, some suggested to be thread safe, some suggested to be using one SessionFactory etc, therefor I am not sure which one is applicable to mine.
Below is a portion of my database layer.
package dao;
import java.util.List;
import model.main.Familyvisa;
import model.main.Familyvisa;
import model.main.Familyvisa;
import model.main.Pensionhistory;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
/**
*
* #author user
*/
public class FamilyVisaImpl implements FamilyVisaInterface
{
private Session currentSession;
private Transaction currentTransaction;
public Session openCurrentSession() {
currentSession = getSessionFactory().openSession();
return currentSession;
}
public Session openCurrentSessionwithTransaction() {
currentSession = getSessionFactory().openSession();
currentTransaction = currentSession.beginTransaction();
return currentSession;
}
public void closeCurrentSession() {
currentSession.close();
}
public void closeCurrentSessionwithTransaction() {
currentTransaction.commit();
currentSession.close();
}
private static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
}
public Session getCurrentSession() {
return currentSession;
}
public void setCurrentSession(Session currentSession) {
this.currentSession = currentSession;
}
public Transaction getCurrentTransaction() {
return currentTransaction;
}
public void setCurrentTransaction(Transaction currentTransaction) {
this.currentTransaction = currentTransaction;
}
#Override
public void save(Familyvisa entity) {
getCurrentSession().save(entity);
}
#Override
public void update(Familyvisa entity) {
getCurrentSession().update(entity);
}
#Override
public Familyvisa findById(int id) {
Familyvisa book = (Familyvisa) getCurrentSession().get(Familyvisa.class, id);
return book;
}
#Override
public void delete(Familyvisa entity) {
getCurrentSession().delete(entity);
}
#Override
public List<Familyvisa> findAll() {
List<Familyvisa> remDur = (List<Familyvisa>) getCurrentSession().createQuery("from Familyvisa").list();
return remDur;
}
public Familyvisa findByForiegnKey_Family(int idFamily)
{
String hql = "FROM Familyvisa WHERE idFamily = :famId";
//String hql = "FROM Visa WHERE idFamily = :famId";
Query q = getCurrentSession().createQuery(hql);
q.setParameter("famId", idFamily);
Familyvisa v = new Familyvisa();
if(!q.list().isEmpty())
{
v = (Familyvisa)q.list().get(0);
}
return v;
}
#Override
public void saveOrUpdate(Familyvisa p)
{
getCurrentSession().saveOrUpdate(p);
}
#Override
public List<Object[]> findReminderActiveVisaWithFamilyAndEmployee()
{
String sql = "";
SQLQuery createSQLQuery = getCurrentSession().createSQLQuery(sql);
return createSQLQuery.list();
}
#Override
public void batchUpdate(List<Familyvisa> list)
{
for(int i=0;i<list.size();i++)
{
getCurrentSession().update(list.get(i));
}
}
}
Below is my service layer, related to the above code.
package service;
import dao.FamilyVisaImpl;
import java.util.List;
import model.main.Familyvisa;
/**
*
* #author user
*/
public class FamilyVisaService
{
private FamilyVisaImpl familyVisaImpl;
public FamilyVisaService()
{
familyVisaImpl = new FamilyVisaImpl();
}
public Familyvisa findByForiegnKey_Family(int idFamily)
{
familyVisaImpl.openCurrentSession();
Familyvisa findByForiegnKey_Family = familyVisaImpl.findByForiegnKey_Family(idFamily);
familyVisaImpl.closeCurrentSession();
return findByForiegnKey_Family;
}
public List<Object[]> findReminderActiveVisaWithFamilyAndEmployee()
{
familyVisaImpl.openCurrentSession();
List<Object[]> visa = familyVisaImpl.findReminderActiveVisaWithFamilyAndEmployee();
familyVisaImpl.closeCurrentSession();
return visa;
}
public void batchUpdate(List<Familyvisa> list)
{
familyVisaImpl.openCurrentSessionwithTransaction();
familyVisaImpl.batchUpdate(list);
familyVisaImpl.closeCurrentSessionwithTransaction();
}
}
Below is a code from Servlet, which explains how I execute the code.
private void updateDatabase(List<VisaWithFamilyAndEmployeeBean> reminderSentList)
{
FamilyVisaService service = new FamilyVisaService();
List<Familyvisa> visa = new ArrayList<Familyvisa>();
for(int i=0;i<reminderSentList.size();i++)
{
Familyvisa familyVisa = service.findByForiegnKey_Family(reminderSentList.get(i).getIdFamily());
familyVisa.setNumberOfReminders(familyVisa.getNumberOfReminders()+1);
familyVisa.setLastReminderSent(Common.getCurrentDateSQL());
visa.add(familyVisa);
}
service.batchUpdate(visa);
}
I have lot of classes in three layers (servlet, DAO, Service) and all follow the exact same structure, serving different purposes, but methods looks almost same (like update, insert etc).
Please pay some decent attention to the code, to the key words, usage of access specifiers etc. In some other classes, in service layer, I define it's IMPL as static as well eg: private static EmployeeImpl employeeimpl;
Can you find what is happening wrong here? Since it occures only "sometimes" and in any of the code (not only in here, but the other classes also same, only difference is they call to different tables) so I can figure it out.
UPDATE
Considering the comments and answers, I changed the code to below. Please let me know whether it is in quality level.
FamilyVisaService service = new FamilyVisaService();
Session session = service.openCurrentSession(); //This method will call openCurrentSession() in Impl class
try {
for(int i=0;i<reminderSentList.size();i++)
{
/* findByForiegnKey_Family() has Session argument now! */
Familyvisa familyVisa = service.findByForiegnKey_Family(session, reminderSentList.get(i).getIdFamily());
familyVisa.setNumberOfReminders(familyVisa.getNumberOfReminders()+1);
familyVisa.setLastReminderSent(Common.getCurrentDateSQL());
visa.add(familyVisa);
}
} catch (Exception ex) {
System.out.println("ERROR:"+ex);
} finally {
session.close();
}
Your snippet of code:
for(int i=0;i<reminderSentList.size();i++)
{
Familyvisa familyVisa = service.findByForiegnKey_Family(reminderSentList.get(i).getIdFamily());
familyVisa.setNumberOfReminders(familyVisa.getNumberOfReminders()+1);
familyVisa.setLastReminderSent(Common.getCurrentDateSQL());
visa.add(familyVisa);
}
opens and closes the session inside the loop multiple times during it's execution using service.findByForeignKey_Family() function.
Session openning and closing could take some time but the loop is fast enough. That's why could multiple sessions to be open: it's just need the time to be closed. And in your code it's actual. That's why "Too Many Connections" error occurs.
In other words, pass the session to the service.findByForiegnKey_Family() as parameter instead of openning and closing this inside function.
Like this:
Session session = ...
try {
for(int i=0;i<reminderSentList.size();i++)
{
/* findByForiegnKey_Family() has Session argument now! */
Familyvisa familyVisa = service.findByForiegnKey_Family(session, reminderSentList.get(i).getIdFamily());
familyVisa.setNumberOfReminders(familyVisa.getNumberOfReminders()+1);
familyVisa.setLastReminderSent(Common.getCurrentDateSQL());
visa.add(familyVisa);
}
} catch (Exception ex) {
System.out.println("ERROR:"+ex);
} finally {
session.close();
}
The above example is Thread-safe. Because you open, operate and close the session inside single function.
Hibernate requires transaction block even for read operations. So you have to fix your code like this:
Session session = ...
try {
session.beginTransaction();
...
Your Loop
...
session.getTransaction.commit();
...
Your code is wrong in so many ways:
The code is not Thread-safe, as you already admitted:
private Session currentSession;
private Transaction currentTransaction;
public Session openCurrentSession() {
currentSession = getSessionFactory().openSession();
return currentSession;
}
public Session openCurrentSessionwithTransaction() {
currentSession = getSessionFactory().openSession();
currentTransaction = currentSession.beginTransaction();
return currentSession;
}
public void closeCurrentSession() {
currentSession.close();
}
public void closeCurrentSessionwithTransaction() {
currentTransaction.commit();
currentSession.close();
}
The Service layer singletons should never store state, because they are accessed by concurrent requests. What if you have a current running Session and a second request opens a new session as well? The first thread will never get the chance to close his session, but it will attempt to close the last opened session (e.g. currentSession).
The Session is not even thread-safe so you will bump into all sorts of weird concurrent modification or change visibility errors.
You should follow the Hibernate session management best practices and choose for a session-per-request solution backed by a ThreadLocal Session storage.
Adding Spring Transaction Management is a simple and effective way of handling the connection/session/transaction management.

JPA and generics, why my class doesn't work?

In my Spring web app I'm using a generic dao class:
public abstract class GenericDaoImpl<T> implements GenericDao<T> {
#Override
public T create(final T t) {
this.getEntityManager().persist(t);
return t;
}
#Override
public void delete(final Object id) {
this.getEntityManager().remove(
this.getEntityManager().getReference(getEntityType(), id));
}
#Override
public T find(final Object id) {
return (T) this.getEntityManager().find(getEntityType(), id);
}
#Override
public T update(final T t) {
return this.getEntityManager().merge(t);
}
}
I implement this class for every entity in my model and it works perfectly. For example:
#Repository
public class GruppoDaoImpl extends GenericDaoImpl<Gruppo> implements GruppoDao {
}
I use these dao classes in my service layer. I have a service layer for every entity in my model, but methods for most of these classes, are the same, so I tried to create a generic service class that I can extend in the same way I do for the generic dao:
public abstract class GenericAdminServiceImpl<ENTITY extends AbstractEntity, DTO extends AbstractDto>
implements GenericAdminService<ENTITY, DTO> {
private GenericDao<ENTITY> dao;
private Class<ENTITY> entityClass;
private Class<DTO> dtoClass;
#SuppressWarnings({ "unchecked", "rawtypes" })
protected GenericAdminServiceImpl(GenericDao<ENTITY> dao) {
this.dao = dao;
//
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
this.entityClass = (Class) pt.getActualTypeArguments()[0];
this.dtoClass = (Class) pt.getActualTypeArguments()[1];
}
public DTO getById(Object id) {
DTO dto = null;
ENTITY entity = dao.find(id);
if (entity != null) {
try {
dto = dtoClass.newInstance();
initDto(entity, dto);
} catch (Exception e) {
}
}
return dto;
}
public void create(DTO dto) throws ServiceOperationException {
ENTITY entity;
try {
entity = entityClass.newInstance();
initEntity(dto, entity);
Date dt = new Date();
entity.setDataUltimoAggiornamento(dt);
entity.setUtenteUltimoAggiornamento(dto.getLoggedUser());
entity.setDataInserimento(dt);
entity.setUtenteInserimento(dto.getLoggedUser());
dao.create(entity);
} catch (Exception e) {
throw new ServiceOperationException("impossibile creare entity ["
+ entityClass.getSimpleName() + "]", e);
}
}
public void update(DTO dto) throws ServiceOperationException {
ENTITY entity = dao.find(dto.getId());
if (!entityExists(entity)) {
throw new ServiceOperationException("entity non esistente ["
+ entityClass.getSimpleName() + "#" + dto.getId() + "]");
}
initEntity(dto, entity);
Date dt = new Date();
entity.setDataUltimoAggiornamento(dt);
entity.setUtenteUltimoAggiornamento(dto.getLoggedUser());
dao.update(entity);
}
public void delete(Object id) throws ServiceOperationException {
try {
dao.delete((int) id);
} catch (Exception e) {
throw new ServiceOperationException(
"impossibile eliminare entity ["
+ entityClass.getSimpleName() + "#" + id + "]", e); // TODO
}
}
protected abstract void initDto(ENTITY entity, DTO outDto);
protected abstract void initEntity(DTO dto, ENTITY outEntity);
protected abstract boolean entityExists(ENTITY entity);
}
Extending this class I just have to implement specific parts for every entity, leaving all the common stuff in the abstract/generic class.
The problem is that using the generic service, merge, persist and delete don't work. Only select seems to work and I cannot understand why...
When I run debug mode in Eclipse all seems correct. A consistent entity is passed to merge/persist methods, so why they don't work? can you help me?
UPDATE #1
This is an example of implementation:
#Service
#Transactional(propagation = Propagation.REQUIRES_NEW)
public class GruppoServiceImplG extends
GenericAdminServiceImpl<Gruppo, GruppoDto> implements GruppoServiceG {
#Autowired
protected GruppoServiceImplG(GruppoDao gruppoDao) {
super(gruppoDao);
}
#Override
protected void initDto(Gruppo entity, GruppoDto outDto) {
outDto.setId(entity.getId());
outDto.setNome(entity.getNome());
outDto.setDescrizione(entity.getDescrizione());
outDto.setDataInizioValidita(entity.getDataInizioValidita());
outDto.setDataFineValidita(entity.getDataFineValidita());
}
#Override
protected void initEntity(GruppoDto dto, Gruppo outEntity) {
outEntity.setId(dto.getId());
outEntity.setNome(dto.getNome());
outEntity.setDescrizione(dto.getDescrizione());
outEntity.setDataInizioValidita(dto.getDataInizioValidita());
outEntity.setDataFineValidita(dto.getDataFineValidita());
}
#Override
protected boolean entityExists(Gruppo entity) {
return entity != null && entity.getId() > 0;
}
}
UPDATE #2
Following Łukasz L. suggestion, I added to all my crud methods a flush(). Now I get this exception javax.persistence.TransactionRequiredException: no transaction is in progress. What's wrong with my transaction declaration? it works fine with non-generic serices...
If you read that question about Spring and Hibernate flush behaviour, it's not easy that commiting your transaction will make also the EntityManager to save all changes. Spring and JPA (Hibernate&CO) are designed to work quite nice (from the Spring side) but nevertheless, you must assert that your entity manager will write all queries to database before commiting transaction.
The problem: JPAs like to cache. It means, they tend to avoid issuing queries. If you do SELECT, they have no choice - they must fetch some data (as long as that data portion was not fetched - like when getting single entity by ID). By INSERTs and UPDATEs - well, they CAN cache. It means, that create, merge or remove will usually not issue a query to RDBMS until you call flush() on EntityManager.
If you leave transactional block without calling flush, and entity manager is delaying operations, you'll commit transactions, by which the modifying queries were not issued!
Just make sure to call EntityManager.flush() at least at the end of the transactional method. You can also call it after each DML operation, it's your choice (I prefer that way because it gives me full control in which order the DML queries are issued by JPA, if you heavily uses DB constraints/triggers, it can be essential).
#Transactional
public void myTransactionalMethod() {
getEntityManager().persist(a); // I've made some JPA change, that is not issued to DB yet
...
// I'm doing something more
...
getEntityManager().flush(); // the last moment to flush, after that instruction I leave transactional context
}
Following Łukasz L. suggestion I discovered the actual issue in my generic class.
Transaction declaration was wrong. I set #Transactional(propagation = Propagation.REQUIRES_NEW) only in concrete service class.
I solved this way:
#Transactional(propagation = Propagation.REQUIRES_NEW)
public abstract class GenericAdminServiceImpl<ENTITY extends AbstractEntity, DTO extends AbstractDto>
implements GenericAdminService<ENTITY, DTO> {
// ...
}
And (in concrete implementation):
#Service
#Transactional
public class GruppoServiceImplG extends
GenericAdminServiceImpl<Gruppo, GruppoDto> implements GruppoServiceG {
// ...
}

Making datastore and blobstore work transactional

I'm usnig Google App Engine with Java, and using Datastore via JPA.
I want to save profile image for each user:
#Entity
public class User implements Serializable {
#Id
private String userId;
private String imageKey; // saves BlobKey.getKeyString()
// ... other fields and getter/setter methods ...
}
If user wants to change their profile image, I (should) update imageKey field AND delete old image associated with old imageKey.
But, the document of Blobstore for Python says:
Deleting a Blobstore value occurs separately from datastore transactions. If you call this method during a datastore transaction, it takes effect immediately, regardless of whether the transaction commits successfully or is retried.
This seems that I can't make updating imageKey and deleting old image as one atomic action, and it also would affect to Java.
This is my attempt to do this work:
public class Engine implements ServletContextListener {
private EntityManagerFactory emf;
private BlobstoreService blobstoreService;
// Servlet will call getUser, modify returned object, and call updateUser with that object
public User getUser(final String userId) throws EngineException {
return doTransaction(new EngineFunc<User>() { // Utility method for create Entity manager and start transaction
#Override
public User run(EntityManager em) throws EngineException {
return em.find(User.class, key);
}
});
}
public void updateUser(final User user) throws EngineException {
doTransaction(new EngineFunc<Void>() {
#Override
public Void run(EntityManager em) throws EngineException {
em.merge(user);
return null;
}
});
user.purgeOldImages(blobstoreService);
}
// ... Other methods ...
}
public class User {
#Transient
private transient Set<String> oldImageList = new CopyOnWriteArraySet<>();
public void setImageKey(String imageKey) {
if (this.imageKey != null) {
oldImageList.add(this.imageKey);
}
this.imageKey = imageKey;
if (imageKey != null) {
oldImageList.remove(imageKey);
}
}
public void purgeOldImages(BlobstoreService blobService) {
Set<BlobKey> toDelete = new HashSet<>();
for (String s : oldImageList) {
toDelete.add(new BlobKey(s));
oldImageList.remove(s);
}
blobService.delete(toDelete.toArray(new BlobKey[0]));
}
// ... Other methods ...
}
I think this is neither "beautiful" nor correct code.
Is there the right way to do this?

Hibernate JUnit method add always return 0 instead last added id

I wanna to test that I can insert correct data to db. For that I use mock for my service layer. I know that method for which I write test work correct. For all methods which select data I get rignt results. But when I try to insert data and return last inserted record id I get 0.
public class SeasonServiceImplTest extends BaseServiceTest {
private static final String SEASON_NAME = "2012-2013";
private SeasonServiceImpl seasonService;
private SeasonDao seasonDao;
private LanguageDao languageDao;
public SeasonServiceImplTest() {
}
#Before
#Override
public void setUp() throws Exception {
super.setUp();
seasonService = createService(SeasonServiceImpl.class);
seasonDao = mock(SeasonDao.class);
when(getDap().getDao(SeasonDao.class)).thenReturn(seasonDao);
languageDao = mock(LanguageDao.class);
when(getDap().getDao(LanguageDao.class)).thenReturn(languageDao);
}
private SeasonBean createSeasonBean(){
SeasonBean seasonBean = new SeasonBean();
Long langId = 1L;
seasonBean.setId(langId);
seasonBean.setSeason(SEASON_NAME);
Map<Long, String> seasonNames = new HashMap<>();
seasonNames.put(langId, SEASON_NAME);
seasonBean.setSeasons(seasonNames);
seasonBean.setBeach(true);
return seasonBean;
}
#Test
public void testAddSeason() throws TechnicalException, LogicalException {
Long idAddedSeason = seasonService.addSeason(createSeasonBean());
SeasonBean selectedSeasonBean = seasonService.getSeasonById(idAddedSeason);
assertNotNull("Season wasn't added", selectedSeasonBean);
}
}
And insert in DAO
//addSeason in DAO layer
#Override
public Long insert(T entity) {
getSession().save(entity);
return (Long) getSession().getIdentifier(entity);
}
Why can be last inserted record id equls 0?

Categories