Related
I have an update query:
#Modifying
#Transactional
#Query("UPDATE Admin SET firstname = :firstname, lastname = :lastname, login = :login, superAdmin = :superAdmin, preferenceAdmin = :preferenceAdmin, address = :address, zipCode = :zipCode, city = :city, country = :country, email = :email, profile = :profile, postLoginUrl = :postLoginUrl WHERE id = :id")
public void update(#Param("firstname") String firstname, #Param("lastname") String lastname, #Param("login") String login, #Param("superAdmin") boolean superAdmin, #Param("preferenceAdmin") boolean preferenceAdmin, #Param("address") String address, #Param("zipCode") String zipCode, #Param("city") String city, #Param("country") String country, #Param("email") String email, #Param("profile") String profile, #Param("postLoginUrl") String postLoginUrl, #Param("id") Long id);
I'm trying to use it in an integration test:
adminRepository.update("Toto", "LeHeros", admin0.getLogin(), admin0.getSuperAdmin(), admin0.getPreferenceAdmin(), admin0.getAddress(), admin0.getZipCode(), admin0.getCity(), admin0.getCountry(), admin0.getEmail(), admin0.getProfile(), admin0.getPostLoginUrl(), admin0.getId());
Admin loadedAdmin = adminRepository.findOne(admin0.getId());
assertEquals("Toto", loadedAdmin.getFirstname());
assertEquals("LeHeros", loadedAdmin.getLastname());
But the fields are not updated and retain their initial values, the test thus failing.
I tried adding a flush right before the findOne query:
adminRepository.flush();
But the failed assertion remained identical.
I can see the update sql statement in the log:
update admin set firstname='Toto', lastname='LeHeros', login='stephane', super_admin=0, preference_admin=0,
address=NULL, zip_code=NULL, city=NULL, country=NULL, email='stephane#thalasoft.com', profile=NULL,
post_login_url=NULL where id=2839
But the log shows no sql that could relate to the finder:
Admin loadedAdmin = adminRepository.findOne(admin0.getId());
The finder sql statement is not making its way to the database.
Is it ignored for some caching reason ?
If I then add a call to the findByEmail and findByLogin finders as in:
adminRepository.update("Toto", "LeHeros", "qwerty", admin0.getSuperAdmin(), admin0.getPreferenceAdmin(), admin0.getAddress(), admin0.getZipCode(), admin0.getCity(), admin0.getCountry(), admin0.getEmail(), admin0.getProfile(), admin0.getPostLoginUrl(), admin0.getId());
Admin loadedAdmin = adminRepository.findOne(admin0.getId());
Admin myadmin = adminRepository.findByEmail(admin0.getEmail());
Admin anadmin = adminRepository.findByLogin("qwerty");
assertEquals("Toto", anadmin.getFirstname());
assertEquals("Toto", myadmin.getFirstname());
assertEquals("Toto", loadedAdmin.getFirstname());
assertEquals("LeHeros", loadedAdmin.getLastname());
then I can see in the log the sql statement being generated:
But the assertion:
assertEquals("Toto", myadmin.getFirstname());
still fails even though the trace shows the same domain object was retrieved:
TRACE [BasicExtractor] found [1037] as column [id14_]
One other thing that puzzles me with this other finder is that it shows a limit 2 clause even though it is supposed to return only one Admin object.
I thought there would always be a limit 1 when returning one domain object. Is this a wrong assumption on Spring Data ?
When pasting in a MySQL client, the sql statements displayed in the console log, the logic works fine:
mysql> insert into admin (version, address, city, country, email, firstname, lastname, login, password,
-> password_salt, post_login_url, preference_admin, profile, super_admin, zip_code) values (0,
-> NULL, NULL, NULL, 'zemail#thalasoft.com039', 'zfirstname039', 'zlastname039', 'zlogin039',
-> 'zpassword039', '', NULL, 0, NULL, 1, NULL);
Query OK, 1 row affected (0.07 sec)
mysql> select * from admin;
+------+---------+---------------+--------------+-----------+--------------+---------------+-------------+------------------+---------+----------+------+---------+-------------------------+---------+----------------+
| id | version | firstname | lastname | login | password | password_salt | super_admin | preference_admin | address | zip_code | city | country | email | profile | post_login_url |
+------+---------+---------------+--------------+-----------+--------------+---------------+-------------+------------------+---------+----------+------+---------+-------------------------+---------+----------------+
| 1807 | 0 | zfirstname039 | zlastname039 | zlogin039 | zpassword039 | | 1 | 0 | NULL | NULL | NULL | NULL | zemail#thalasoft.com039 | NULL | NULL |
+------+---------+---------------+--------------+-----------+--------------+---------------+-------------+------------------+---------+----------+------+---------+-------------------------+---------+----------------+
1 row in set (0.00 sec)
mysql> update admin set firstname='Toto', lastname='LeHeros', login='qwerty', super_admin=0, preference_admin=0, address=NULL, zip_code=NULL, city=NULL, country=NULL, email='stephane#thalasoft.com', profile=NULL, post_login_url=NULL where id=1807;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from admin; +------+---------+-----------+----------+--------+--------------+---------------+-------------+------------------+---------+----------+------+---------+------------------------+---------+----------------+
| id | version | firstname | lastname | login | password | password_salt | super_admin | preference_admin | address | zip_code | city | country | email | profile | post_login_url |
+------+---------+-----------+----------+--------+--------------+---------------+-------------+------------------+---------+----------+------+---------+------------------------+---------+----------------+
| 1807 | 0 | Toto | LeHeros | qwerty | zpassword039 | | 0 | 0 | NULL | NULL | NULL | NULL | stephane#thalasoft.com | NULL | NULL |
+------+---------+-----------+----------+--------+--------------+---------------+-------------+------------------+---------+----------+------+---------+------------------------+---------+----------------+
1 row in set (0.00 sec)
mysql> select admin0_.id as id14_, admin0_.version as version14_, admin0_.address as address14_, admin0_.city as city14_, admin0_.country as country14_, admin0_.email as email14_, admin0_.firstname as firstname14_, admin0_.lastname as lastname14_, admin0_.login as login14_, admin0_.password as password14_, admin0_.password_salt as password11_14_, admin0_.post_login_url as post12_14_, admin0_.preference_admin as preference13_14_, admin0_.profile as profile14_, admin0_.super_admin as super15_14_, admin0_.zip_code as zip16_14_ from admin admin0_ where admin0_.email='stephane#thalasoft.com' limit 2;
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
| id14_ | version14_ | address14_ | city14_ | country14_ | email14_ | firstname14_ | lastname14_ | login14_ | password14_ | password11_14_ | post12_14_ | preference13_14_ | profile14_ | super15_14_ | zip16_14_ |
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
| 1807 | 0 | NULL | NULL | NULL | stephane#thalasoft.com | Toto | LeHeros | qwerty | zpassword039 | | NULL | 0 | NULL | 0 | NULL |
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
1 row in set (0.00 sec)
mysql> select admin0_.id as id14_, admin0_.version as version14_, admin0_.address as address14_, admin0_.city as city14_, admin0_.country as country14_, admin0_.email as email14_, admin0_.firstname as firstname14_, admin0_.lastname as lastname14_, admin0_.login as login14_, admin0_.password as password14_, admin0_.password_salt as password11_14_, admin0_.post_login_url as post12_14_, admin0_.preference_admin as preference13_14_, admin0_.profile as profile14_, admin0_.super_admin as super15_14_, admin0_.zip_code as zip16_14_ from admin admin0_ where admin0_.login='qwerty' limit 2;
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
| id14_ | version14_ | address14_ | city14_ | country14_ | email14_ | firstname14_ | lastname14_ | login14_ | password14_ | password11_14_ | post12_14_ | preference13_14_ | profile14_ | super15_14_ | zip16_14_ |
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
| 1807 | 0 | NULL | NULL | NULL | stephane#thalasoft.com | Toto | LeHeros | qwerty | zpassword039 | | NULL | 0 | NULL | 0 | NULL |
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
1 row in set (0.00 sec)
So why is this not reflected at the Java level ?
The EntityManager doesn't flush change automatically by default. You should use the following option with your statement of query:
#Modifying(clearAutomatically = true)
#Query("update RssFeedEntry feedEntry set feedEntry.read =:isRead where feedEntry.id =:entryId")
void markEntryAsRead(#Param("entryId") Long rssFeedEntryId, #Param("isRead") boolean isRead);
I finally understood what was going on.
When creating an integration test on a statement saving an object, it is recommended to flush the entity manager so as to avoid any false negative, that is, to avoid a test running fine but whose operation would fail when run in production. Indeed, the test may run fine simply because the first level cache is not flushed and no writing hits the database. To avoid this false negative integration test use an explicit flush in the test body. Note that the production code should never need to use any explicit flush as it is the role of the ORM to decide when to flush.
When creating an integration test on an update statement, it may be necessary to clear the entity manager so as to reload the first level cache. Indeed, an update statement completely bypasses the first level cache and writes directly to the database. The first level cache is then out of sync and reflects the old value of the updated object. To avoid this stale state of the object, use an explicit clear in the test body. Note that the production code should never need to use any explicit clear as it is the role of the ORM to decide when to clear.
My test now works just fine.
I was able to get this to work. I will describe my application and the integration test here.
The Example Application
The example application has two classes and one interface that are relevant to this problem:
The application context configuration class
The entity class
The repository interface
These classes and the repository interface are described in the following.
The source code of the PersistenceContext class looks as follows:
import com.jolbox.bonecp.BoneCPDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(basePackages = "net.petrikainulainen.spring.datajpa.todo.repository")
#PropertySource("classpath:application.properties")
public class PersistenceContext {
protected static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
protected static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
protected static final String PROPERTY_NAME_DATABASE_URL = "db.url";
protected static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY = "hibernate.ejb.naming_strategy";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_PACKAGES_TO_SCAN = "net.petrikainulainen.spring.datajpa.todo.model";
#Autowired
private Environment environment;
#Bean
public DataSource dataSource() {
BoneCPDataSource dataSource = new BoneCPDataSource();
dataSource.setDriverClass(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
dataSource.setJdbcUrl(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
dataSource.setUsername(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
dataSource.setPassword(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
return dataSource;
}
#Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan(PROPERTY_PACKAGES_TO_SCAN);
Properties jpaProperties = new Properties();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
}
Let's assume that we have a simple entity called Todo which source code looks as follows:
#Entity
#Table(name="todos")
public class Todo {
public static final int MAX_LENGTH_DESCRIPTION = 500;
public static final int MAX_LENGTH_TITLE = 100;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "description", nullable = true, length = MAX_LENGTH_DESCRIPTION)
private String description;
#Column(name = "title", nullable = false, length = MAX_LENGTH_TITLE)
private String title;
#Version
private long version;
}
Our repository interface has a single method called updateTitle() which updates the title of a todo entry. The source code of the TodoRepository interface looks as follows:
import net.petrikainulainen.spring.datajpa.todo.model.Todo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface TodoRepository extends JpaRepository<Todo, Long> {
#Modifying
#Query("Update Todo t SET t.title=:title WHERE t.id=:id")
public void updateTitle(#Param("id") Long id, #Param("title") String title);
}
The updateTitle() method is not annotated with the #Transactional annotation because I think that it is best to use a service layer as a transaction boundary.
The Integration Test
The Integration Test uses DbUnit, Spring Test and Spring-Test-DBUnit. It has three components which are relevant to this problem:
The DbUnit dataset which is used to initialize the database into a known state before the test is executed.
The DbUnit dataset which is used to verify that the title of the entity is updated.
The integration test.
These components are described with more details in the following.
The name of the DbUnit dataset file which is used to initialize the database to known state is toDoData.xml and its content looks as follows:
<dataset>
<todos id="1" description="Lorem ipsum" title="Foo" version="0"/>
<todos id="2" description="Lorem ipsum" title="Bar" version="0"/>
</dataset>
The name of the DbUnit dataset which is used to verify that the title of the todo entry is updated is called toDoData-update.xml and its content looks as follows (for some reason the version of the todo entry was not updated but the title was. Any ideas why?):
<dataset>
<todos id="1" description="Lorem ipsum" title="FooBar" version="0"/>
<todos id="2" description="Lorem ipsum" title="Bar" version="0"/>
</dataset>
The source code of the actual integration test looks as follows (Remember to annotate the test method with the #Transactional annotation):
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.ExpectedDatabase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {PersistenceContext.class})
#TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
#DatabaseSetup("todoData.xml")
public class ITTodoRepositoryTest {
#Autowired
private TodoRepository repository;
#Test
#Transactional
#ExpectedDatabase("toDoData-update.xml")
public void updateTitle_ShouldUpdateTitle() {
repository.updateTitle(1L, "FooBar");
}
}
After I run the integration test, the test passes and the title of the todo entry is updated. The only problem which I am having is that the version field is not updated. Any ideas why?
I undestand that this description is a bit vague. If you want to get more information about writing integration tests for Spring Data JPA repositories, you can read my blog post about it.
The underlying problem here is the 1st level cache of JPA.
From the JPA spec Version 2.2 section 3.1. emphasise is mine:
An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance.
This is important because JPA tracks changes to that entity in order to flush them to the database.
As a side effect it also means within a single persistence context an entity gets only loaded once.
This why reloading the changed entity doesn't have any effect.
You have a couple of options how to handle this:
Evict the entity from the EntityManager.
This may be done by calling EntityManager.detach, annotating the updating method with #Modifying(clearAutomatically = true) which evicts all entities.
Make sure changes to these entities get flushed first or you might end up loosing changes.
Use EntityManager.refresh().
Use a different persistence context to load the entity.
The easiest way to do this is to do it in a separate transaction.
With Spring this can be done by having separate methods annotated with #Transactional on beans called from a bean not annotated with #Transactional.
Another way is to use a TransactionTemplate which works especially nicely in tests where it makes transaction boundaries very visible.
I struggled with the same problem where I was trying to execute an update query like the same as you did-
#Modifying
#Transactional
#Query(value = "UPDATE SAMPLE_TABLE st SET st.status=:flag WHERE se.referenceNo in :ids")
public int updateStatus(#Param("flag")String flag, #Param("ids")List<String> references);
This will work if you have put #EnableTransactionManagement annotation on the main class.
Spring 3.1 introduces the #EnableTransactionManagement annotation to be used in on #Configuration classes and enable transactional support.
I have an update query:
#Modifying
#Transactional
#Query("UPDATE Admin SET firstname = :firstname, lastname = :lastname, login = :login, superAdmin = :superAdmin, preferenceAdmin = :preferenceAdmin, address = :address, zipCode = :zipCode, city = :city, country = :country, email = :email, profile = :profile, postLoginUrl = :postLoginUrl WHERE id = :id")
public void update(#Param("firstname") String firstname, #Param("lastname") String lastname, #Param("login") String login, #Param("superAdmin") boolean superAdmin, #Param("preferenceAdmin") boolean preferenceAdmin, #Param("address") String address, #Param("zipCode") String zipCode, #Param("city") String city, #Param("country") String country, #Param("email") String email, #Param("profile") String profile, #Param("postLoginUrl") String postLoginUrl, #Param("id") Long id);
I'm trying to use it in an integration test:
adminRepository.update("Toto", "LeHeros", admin0.getLogin(), admin0.getSuperAdmin(), admin0.getPreferenceAdmin(), admin0.getAddress(), admin0.getZipCode(), admin0.getCity(), admin0.getCountry(), admin0.getEmail(), admin0.getProfile(), admin0.getPostLoginUrl(), admin0.getId());
Admin loadedAdmin = adminRepository.findOne(admin0.getId());
assertEquals("Toto", loadedAdmin.getFirstname());
assertEquals("LeHeros", loadedAdmin.getLastname());
But the fields are not updated and retain their initial values, the test thus failing.
I tried adding a flush right before the findOne query:
adminRepository.flush();
But the failed assertion remained identical.
I can see the update sql statement in the log:
update admin set firstname='Toto', lastname='LeHeros', login='stephane', super_admin=0, preference_admin=0,
address=NULL, zip_code=NULL, city=NULL, country=NULL, email='stephane#thalasoft.com', profile=NULL,
post_login_url=NULL where id=2839
But the log shows no sql that could relate to the finder:
Admin loadedAdmin = adminRepository.findOne(admin0.getId());
The finder sql statement is not making its way to the database.
Is it ignored for some caching reason ?
If I then add a call to the findByEmail and findByLogin finders as in:
adminRepository.update("Toto", "LeHeros", "qwerty", admin0.getSuperAdmin(), admin0.getPreferenceAdmin(), admin0.getAddress(), admin0.getZipCode(), admin0.getCity(), admin0.getCountry(), admin0.getEmail(), admin0.getProfile(), admin0.getPostLoginUrl(), admin0.getId());
Admin loadedAdmin = adminRepository.findOne(admin0.getId());
Admin myadmin = adminRepository.findByEmail(admin0.getEmail());
Admin anadmin = adminRepository.findByLogin("qwerty");
assertEquals("Toto", anadmin.getFirstname());
assertEquals("Toto", myadmin.getFirstname());
assertEquals("Toto", loadedAdmin.getFirstname());
assertEquals("LeHeros", loadedAdmin.getLastname());
then I can see in the log the sql statement being generated:
But the assertion:
assertEquals("Toto", myadmin.getFirstname());
still fails even though the trace shows the same domain object was retrieved:
TRACE [BasicExtractor] found [1037] as column [id14_]
One other thing that puzzles me with this other finder is that it shows a limit 2 clause even though it is supposed to return only one Admin object.
I thought there would always be a limit 1 when returning one domain object. Is this a wrong assumption on Spring Data ?
When pasting in a MySQL client, the sql statements displayed in the console log, the logic works fine:
mysql> insert into admin (version, address, city, country, email, firstname, lastname, login, password,
-> password_salt, post_login_url, preference_admin, profile, super_admin, zip_code) values (0,
-> NULL, NULL, NULL, 'zemail#thalasoft.com039', 'zfirstname039', 'zlastname039', 'zlogin039',
-> 'zpassword039', '', NULL, 0, NULL, 1, NULL);
Query OK, 1 row affected (0.07 sec)
mysql> select * from admin;
+------+---------+---------------+--------------+-----------+--------------+---------------+-------------+------------------+---------+----------+------+---------+-------------------------+---------+----------------+
| id | version | firstname | lastname | login | password | password_salt | super_admin | preference_admin | address | zip_code | city | country | email | profile | post_login_url |
+------+---------+---------------+--------------+-----------+--------------+---------------+-------------+------------------+---------+----------+------+---------+-------------------------+---------+----------------+
| 1807 | 0 | zfirstname039 | zlastname039 | zlogin039 | zpassword039 | | 1 | 0 | NULL | NULL | NULL | NULL | zemail#thalasoft.com039 | NULL | NULL |
+------+---------+---------------+--------------+-----------+--------------+---------------+-------------+------------------+---------+----------+------+---------+-------------------------+---------+----------------+
1 row in set (0.00 sec)
mysql> update admin set firstname='Toto', lastname='LeHeros', login='qwerty', super_admin=0, preference_admin=0, address=NULL, zip_code=NULL, city=NULL, country=NULL, email='stephane#thalasoft.com', profile=NULL, post_login_url=NULL where id=1807;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from admin; +------+---------+-----------+----------+--------+--------------+---------------+-------------+------------------+---------+----------+------+---------+------------------------+---------+----------------+
| id | version | firstname | lastname | login | password | password_salt | super_admin | preference_admin | address | zip_code | city | country | email | profile | post_login_url |
+------+---------+-----------+----------+--------+--------------+---------------+-------------+------------------+---------+----------+------+---------+------------------------+---------+----------------+
| 1807 | 0 | Toto | LeHeros | qwerty | zpassword039 | | 0 | 0 | NULL | NULL | NULL | NULL | stephane#thalasoft.com | NULL | NULL |
+------+---------+-----------+----------+--------+--------------+---------------+-------------+------------------+---------+----------+------+---------+------------------------+---------+----------------+
1 row in set (0.00 sec)
mysql> select admin0_.id as id14_, admin0_.version as version14_, admin0_.address as address14_, admin0_.city as city14_, admin0_.country as country14_, admin0_.email as email14_, admin0_.firstname as firstname14_, admin0_.lastname as lastname14_, admin0_.login as login14_, admin0_.password as password14_, admin0_.password_salt as password11_14_, admin0_.post_login_url as post12_14_, admin0_.preference_admin as preference13_14_, admin0_.profile as profile14_, admin0_.super_admin as super15_14_, admin0_.zip_code as zip16_14_ from admin admin0_ where admin0_.email='stephane#thalasoft.com' limit 2;
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
| id14_ | version14_ | address14_ | city14_ | country14_ | email14_ | firstname14_ | lastname14_ | login14_ | password14_ | password11_14_ | post12_14_ | preference13_14_ | profile14_ | super15_14_ | zip16_14_ |
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
| 1807 | 0 | NULL | NULL | NULL | stephane#thalasoft.com | Toto | LeHeros | qwerty | zpassword039 | | NULL | 0 | NULL | 0 | NULL |
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
1 row in set (0.00 sec)
mysql> select admin0_.id as id14_, admin0_.version as version14_, admin0_.address as address14_, admin0_.city as city14_, admin0_.country as country14_, admin0_.email as email14_, admin0_.firstname as firstname14_, admin0_.lastname as lastname14_, admin0_.login as login14_, admin0_.password as password14_, admin0_.password_salt as password11_14_, admin0_.post_login_url as post12_14_, admin0_.preference_admin as preference13_14_, admin0_.profile as profile14_, admin0_.super_admin as super15_14_, admin0_.zip_code as zip16_14_ from admin admin0_ where admin0_.login='qwerty' limit 2;
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
| id14_ | version14_ | address14_ | city14_ | country14_ | email14_ | firstname14_ | lastname14_ | login14_ | password14_ | password11_14_ | post12_14_ | preference13_14_ | profile14_ | super15_14_ | zip16_14_ |
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
| 1807 | 0 | NULL | NULL | NULL | stephane#thalasoft.com | Toto | LeHeros | qwerty | zpassword039 | | NULL | 0 | NULL | 0 | NULL |
+-------+------------+------------+---------+------------+------------------------+--------------+-------------+----------+--------------+----------------+------------+------------------+------------+-------------+-----------+
1 row in set (0.00 sec)
So why is this not reflected at the Java level ?
The EntityManager doesn't flush change automatically by default. You should use the following option with your statement of query:
#Modifying(clearAutomatically = true)
#Query("update RssFeedEntry feedEntry set feedEntry.read =:isRead where feedEntry.id =:entryId")
void markEntryAsRead(#Param("entryId") Long rssFeedEntryId, #Param("isRead") boolean isRead);
I finally understood what was going on.
When creating an integration test on a statement saving an object, it is recommended to flush the entity manager so as to avoid any false negative, that is, to avoid a test running fine but whose operation would fail when run in production. Indeed, the test may run fine simply because the first level cache is not flushed and no writing hits the database. To avoid this false negative integration test use an explicit flush in the test body. Note that the production code should never need to use any explicit flush as it is the role of the ORM to decide when to flush.
When creating an integration test on an update statement, it may be necessary to clear the entity manager so as to reload the first level cache. Indeed, an update statement completely bypasses the first level cache and writes directly to the database. The first level cache is then out of sync and reflects the old value of the updated object. To avoid this stale state of the object, use an explicit clear in the test body. Note that the production code should never need to use any explicit clear as it is the role of the ORM to decide when to clear.
My test now works just fine.
I was able to get this to work. I will describe my application and the integration test here.
The Example Application
The example application has two classes and one interface that are relevant to this problem:
The application context configuration class
The entity class
The repository interface
These classes and the repository interface are described in the following.
The source code of the PersistenceContext class looks as follows:
import com.jolbox.bonecp.BoneCPDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(basePackages = "net.petrikainulainen.spring.datajpa.todo.repository")
#PropertySource("classpath:application.properties")
public class PersistenceContext {
protected static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
protected static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
protected static final String PROPERTY_NAME_DATABASE_URL = "db.url";
protected static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY = "hibernate.ejb.naming_strategy";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_PACKAGES_TO_SCAN = "net.petrikainulainen.spring.datajpa.todo.model";
#Autowired
private Environment environment;
#Bean
public DataSource dataSource() {
BoneCPDataSource dataSource = new BoneCPDataSource();
dataSource.setDriverClass(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
dataSource.setJdbcUrl(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
dataSource.setUsername(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
dataSource.setPassword(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
return dataSource;
}
#Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan(PROPERTY_PACKAGES_TO_SCAN);
Properties jpaProperties = new Properties();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
}
Let's assume that we have a simple entity called Todo which source code looks as follows:
#Entity
#Table(name="todos")
public class Todo {
public static final int MAX_LENGTH_DESCRIPTION = 500;
public static final int MAX_LENGTH_TITLE = 100;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "description", nullable = true, length = MAX_LENGTH_DESCRIPTION)
private String description;
#Column(name = "title", nullable = false, length = MAX_LENGTH_TITLE)
private String title;
#Version
private long version;
}
Our repository interface has a single method called updateTitle() which updates the title of a todo entry. The source code of the TodoRepository interface looks as follows:
import net.petrikainulainen.spring.datajpa.todo.model.Todo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface TodoRepository extends JpaRepository<Todo, Long> {
#Modifying
#Query("Update Todo t SET t.title=:title WHERE t.id=:id")
public void updateTitle(#Param("id") Long id, #Param("title") String title);
}
The updateTitle() method is not annotated with the #Transactional annotation because I think that it is best to use a service layer as a transaction boundary.
The Integration Test
The Integration Test uses DbUnit, Spring Test and Spring-Test-DBUnit. It has three components which are relevant to this problem:
The DbUnit dataset which is used to initialize the database into a known state before the test is executed.
The DbUnit dataset which is used to verify that the title of the entity is updated.
The integration test.
These components are described with more details in the following.
The name of the DbUnit dataset file which is used to initialize the database to known state is toDoData.xml and its content looks as follows:
<dataset>
<todos id="1" description="Lorem ipsum" title="Foo" version="0"/>
<todos id="2" description="Lorem ipsum" title="Bar" version="0"/>
</dataset>
The name of the DbUnit dataset which is used to verify that the title of the todo entry is updated is called toDoData-update.xml and its content looks as follows (for some reason the version of the todo entry was not updated but the title was. Any ideas why?):
<dataset>
<todos id="1" description="Lorem ipsum" title="FooBar" version="0"/>
<todos id="2" description="Lorem ipsum" title="Bar" version="0"/>
</dataset>
The source code of the actual integration test looks as follows (Remember to annotate the test method with the #Transactional annotation):
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.ExpectedDatabase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {PersistenceContext.class})
#TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
#DatabaseSetup("todoData.xml")
public class ITTodoRepositoryTest {
#Autowired
private TodoRepository repository;
#Test
#Transactional
#ExpectedDatabase("toDoData-update.xml")
public void updateTitle_ShouldUpdateTitle() {
repository.updateTitle(1L, "FooBar");
}
}
After I run the integration test, the test passes and the title of the todo entry is updated. The only problem which I am having is that the version field is not updated. Any ideas why?
I undestand that this description is a bit vague. If you want to get more information about writing integration tests for Spring Data JPA repositories, you can read my blog post about it.
The underlying problem here is the 1st level cache of JPA.
From the JPA spec Version 2.2 section 3.1. emphasise is mine:
An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance.
This is important because JPA tracks changes to that entity in order to flush them to the database.
As a side effect it also means within a single persistence context an entity gets only loaded once.
This why reloading the changed entity doesn't have any effect.
You have a couple of options how to handle this:
Evict the entity from the EntityManager.
This may be done by calling EntityManager.detach, annotating the updating method with #Modifying(clearAutomatically = true) which evicts all entities.
Make sure changes to these entities get flushed first or you might end up loosing changes.
Use EntityManager.refresh().
Use a different persistence context to load the entity.
The easiest way to do this is to do it in a separate transaction.
With Spring this can be done by having separate methods annotated with #Transactional on beans called from a bean not annotated with #Transactional.
Another way is to use a TransactionTemplate which works especially nicely in tests where it makes transaction boundaries very visible.
I struggled with the same problem where I was trying to execute an update query like the same as you did-
#Modifying
#Transactional
#Query(value = "UPDATE SAMPLE_TABLE st SET st.status=:flag WHERE se.referenceNo in :ids")
public int updateStatus(#Param("flag")String flag, #Param("ids")List<String> references);
This will work if you have put #EnableTransactionManagement annotation on the main class.
Spring 3.1 introduces the #EnableTransactionManagement annotation to be used in on #Configuration classes and enable transactional support.
I want to get the names of all the public methods (void return type and no arguments) of a class1 which is dependent on some other class2.
I am loading class through UrlClassLoader. Now when i am calling getDeclaredMethods, it is throwing NoClassDefFoundError caused by ClassNotFoundException.
I am having 3 mvn modules as
SampleClassLoader: Using it to get the methods of class of Module1.
Module1: Its class using the reference to classes of Module2. And has a dependency of Module2 in its pom.xml also.
Module2
The whole module structure looks like:
Project Structure
ClassLoadingTest
|----- Module1
| |--- pom.xml
| |--- src/main/java/
| | |--- com.classloadingtest.module1
| | |
| | |--- Module1Class1.java
| | |--- Module1Class2.java
|
|----- Module2
| |--- pom.xml
| |--- src/main/java/
| | |--- com.classloadingtest.module2
| | |
| | |--- Module2Class.java
|
|----- SampleClassLoader
| |--- pom.xml
| |--- src/main/java/
| | |--- com.classloadingtest.sampleClassLoader
| | |
| | |--- SampleClassLoader.java
Module1Class1.java
public class Module1Class1 {
public void claas1Fun() {
Module2Class module2ClassObj = new Module2Class();
module2ClassObj.module2Fun();
}
}
Module1Class2.java
public class Module1Class2 {
public void class2Fun(){
try {
Module2Class module2ClassObj = new Module2Class();
module2ClassObj.module2Fun();
} catch(Exception e ){
}
}
}
Module2Class.java
public class Module2Class {
public void module2Fun(){
}
}
SampleClassLoader.java
public class SampleClassLoader {
public static void main(String[] args) {
try {
URL mainSourceClassPathURL = new URL("file:" + System.getProperty("user.dir") + "/ClassLoadingTest/Module1/target/classes/");
URL[] urls = { mainSourceClassPathURL};
ClassLoader classLoader = URLClassLoader.newInstance(urls);
Class<?> testCaseClass = classLoader.loadClass("com.classloadingtest.module1.Module1Class1");
Method method[] = testCaseClass.getDeclaredMethods();
for (int i = 0 ; i < method.length ; i++) {
System.out.println(method[i].getName());
}
} catch (Exception e){
e.printStackTrace();
}
}
}
Now, When Running the SampleClassLoader for class Module1Class1 prints
claas1Fun
But when running for class Module1Class2 it is giving NoClassDefFoundError as:
Exception in thread "main" java.lang.NoClassDefFoundError: com/classloadingtest/module2/Module2Class
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
at com.classloadingtest.sampleClassLoader.SampleClassLoader.main(SampleClassLoader.java:26)
Caused by: java.lang.ClassNotFoundException: com.classloadingtest.module2.Module2Class
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:814)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more
I am having two questions here that is:
When using try catch, why it is giving error?
If the class1 is already loaded at classLoader.loadClass then why getDeclaredMethods method need to load dependent classes?
Concerning the try-catch issue, the point is that java.lang.NoClassDefFoundError is not an Exception, is an Error which is a more severe kind of Throwable.
Errors are, generally speaking, unrecoverable (Like OutOfMemoryError or StackOverflowError, ...), so they are seldom catched.
If you want to catch NoClassDefFoundError you should add catch(NoClassDefFoundError e) to your try
I have 2 applications. App 1 is using JHipster 5.8.2 which I deploy to a Digital Ocean's droplet with 2GB RAM and, after pushing my image to gitlab's registry, I run it docker-compose -f app.yml up (which has _JAVA_OPTIONS=-Xmx512m -Xms256m) and everything runs perfect in about 45 seconds.
App 2 was generated with JHipster 6.0.1 (it's actually an upgrade of my App 1). I used a similar droplet from App 1 for my App 2: single core with 2GB RAM but it failed because of Java Heap Space. After this I changed my droplet to 4GB 2 core droplet and changed my app.yml config to this: _JAVA_OPTIONS=-Xmx3072m -Xms2048m but it still fails with the same issue and after 40 minutes.
After this error I tried running the image in my computer with 32GB, after 2 1/2 hours, the same issue arose.
This is how I packed my 6.0.1 app:
/mvnw verify -Pprod -DskipTests
./mvnw jib:build -Dimage=registry.gitlab.com/amatos/project
In my droplet:
docker-compose -f app.yml up -d
This should work, but it never passes the line Web application fully configured and after several minutes (depending on the amount of RAM), it fails.
Is there an extra step I'm missing?
FINDING:
After doing a lot of testing, I discovered that, by adding a custom DTO I get the Java heap space issue.
Generated DTO:
package com.facturapp.service.dto;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Lob;
/**
* A DTO for the {#link com.facturapp.domain.Address} entity.
*/
public class AddressDTO implements Serializable {
private Long id;
#NotNull
private String name;
private String contact;
private String mobile;
private String address;
#Lob
private String note;
private Boolean delivery;
private Boolean invoicing;
private Boolean active;
private Long districtId;
private String districtName;
private Long partnerId;
private String partnerName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Boolean isDelivery() {
return delivery;
}
public void setDelivery(Boolean delivery) {
this.delivery = delivery;
}
public Boolean isInvoicing() {
return invoicing;
}
public void setInvoicing(Boolean invoicing) {
this.invoicing = invoicing;
}
public Boolean isActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public Long getDistrictId() {
return districtId;
}
public void setDistrictId(Long districtId) {
this.districtId = districtId;
}
public String getDistrictName() {
return districtName;
}
public void setDistrictName(String districtName) {
this.districtName = districtName;
}
public Long getPartnerId() {
return partnerId;
}
public void setPartnerId(Long partnerId) {
this.partnerId = partnerId;
}
public String getPartnerName() {
return partnerName;
}
public void setPartnerName(String partnerName) {
this.partnerName = partnerName;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AddressDTO addressDTO = (AddressDTO) o;
if (addressDTO.getId() == null || getId() == null) {
return false;
}
return Objects.equals(getId(), addressDTO.getId());
}
#Override
public int hashCode() {
return Objects.hashCode(getId());
}
#Override
public String toString() {
return "AddressDTO{" +
"id=" + getId() +
", name='" + getName() + "'" +
", contact='" + getContact() + "'" +
", mobile='" + getMobile() + "'" +
", address='" + getAddress() + "'" +
", note='" + getNote() + "'" +
", delivery='" + isDelivery() + "'" +
", invoicing='" + isInvoicing() + "'" +
", active='" + isActive() + "'" +
", district=" + getDistrictId() +
", district='" + getDistrictName() + "'" +
", partner=" + getPartnerId() +
", partner='" + getPartnerName() + "'" +
"}";
}
}
Custom DTO
package com.facturapp.service.dto;
import com.facturapp.domain.District;
/**
* A DTO for the {#link com.facturapp.domain.Address} entity.
*/
public class AddressFaDTO extends AddressDTO {
private District district;
public District getDistrict() {
return district;
}
public void setDistrict(District district) {
this.district = district;
}
}
The custom DTO is Address and extends the generated DTO. The generated DTO has a few fields, 2 of them are districtId (Long) and districtName (String), but my custom DTO also has district (District). The class District has a property Province which also has Region, which also has Country. So when I retrieve an Address I also get the district, province, region and country.
My custom Resource calls a custom service that retrieves my custom DTO with the district and all the other classes.
If I use the generated DTO I have no issue, but when I use my custom DTO, the app doesn't run and fails due to the Java Heap space.
Error logs
facturapp_1 | 2019-06-03 06:56:20.395 INFO 1 --- [ restartedMain] com.almasoft.facturapp.FacturApp : Starting FacturApp on c40efbe18b21 with PID 1 (/app/classes started by root in /)
facturapp_1 | 2019-06-03 06:56:20.407 INFO 1 --- [ restartedMain] com.almasoft.facturapp.FacturApp : The following profiles are active: prod,swagger
facturapp_1 | 2019-06-03 06:56:45.918 WARN 1 --- [ restartedMain] i.g.j.c.liquibase.AsyncSpringLiquibase : Warning, Liquibase took more than 5 seconds to start up!
facturapp_1 | 2019-06-03 06:56:57.351 INFO 1 --- [ restartedMain] c.a.facturapp.config.WebConfigurer : Web application configuration, using profiles: prod
facturapp_1 | 2019-06-03 06:56:57.353 INFO 1 --- [ restartedMain] c.a.facturapp.config.WebConfigurer : Web application fully configured
facturapp_1 | WARNING: An illegal reflective access operation has occurred
facturapp_1 | WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils (file:/app/libs/spring-core-5.1.6.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
facturapp_1 | WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils
facturapp_1 | WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
facturapp_1 | WARNING: All illegal access operations will be denied in a future release
facturapp_1 | 2019-06-03 07:03:27.207 WARN 1 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.OutOfMemoryError: Java heap space
facturapp_1 | 2019-06-03 07:03:27.606 ERROR 1 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
facturapp_1 |
facturapp_1 | org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.OutOfMemoryError: Java heap space
facturapp_1 | at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:185)
facturapp_1 | at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53)
facturapp_1 | at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360)
facturapp_1 | at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158)
facturapp_1 | at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122)
facturapp_1 | at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:893)
facturapp_1 | at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:163)
facturapp_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552)
facturapp_1 | at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
facturapp_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
facturapp_1 | at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
facturapp_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
facturapp_1 | at com.facturapp.FacturApp.main(FacturApp.java:63)
facturapp_1 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
facturapp_1 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
facturapp_1 | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
facturapp_1 | at java.base/java.lang.reflect.Method.invoke(Unknown Source)
facturapp_1 | at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
facturapp_1 | Caused by: com.google.common.util.concurrent.ExecutionError: java.lang.OutOfMemoryError: Java heap space
facturapp_1 | at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2049)
facturapp_1 | at com.google.common.cache.LocalCache.get(LocalCache.java:3953)
facturapp_1 | at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3976)
facturapp_1 | at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4960)
facturapp_1 | at springfox.documentation.schema.CachingModelDependencyProvider.dependentModels(CachingModelDependencyProvider.java:58)
facturapp_1 | at springfox.documentation.schema.DefaultModelProvider.dependencies(DefaultModelProvider.java:128)
facturapp_1 | at springfox.documentation.schema.CachingModelProvider.dependencies(CachingModelProvider.java:68)
facturapp_1 | at springfox.documentation.spring.web.scanners.ApiModelReader.populateDependencies(ApiModelReader.java:136)
facturapp_1 | at springfox.documentation.spring.web.scanners.ApiModelReader.read(ApiModelReader.java:78)
facturapp_1 | at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:133)
facturapp_1 | at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:71)
facturapp_1 | at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.scanDocumentation(DocumentationPluginsBootstrapper.java:101)
facturapp_1 | at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167)
facturapp_1 | at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182)
facturapp_1 | ... 17 common frames omitted
facturapp_1 | Caused by: java.lang.OutOfMemoryError: Java heap space
facturapp_1 | at java.base/java.util.Arrays.copyOf(Unknown Source)
facturapp_1 | at java.base/java.util.ArrayList.grow(Unknown Source)
facturapp_1 | at java.base/java.util.ArrayList.addAll(Unknown Source)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedPropertiesAndFields(DefaultModelDependencyProvider.java:181)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedDependencies(DefaultModelDependencyProvider.java:120)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.maybeFromRegularType(DefaultModelDependencyProvider.java:207)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedPropertiesAndFields(DefaultModelDependencyProvider.java:183)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedDependencies(DefaultModelDependencyProvider.java:120)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.maybeFromRegularType(DefaultModelDependencyProvider.java:207)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedPropertiesAndFields(DefaultModelDependencyProvider.java:183)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedDependencies(DefaultModelDependencyProvider.java:120)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.maybeFromCollectionElementType(DefaultModelDependencyProvider.java:220)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedPropertiesAndFields(DefaultModelDependencyProvider.java:181)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedDependencies(DefaultModelDependencyProvider.java:120)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.maybeFromCollectionElementType(DefaultModelDependencyProvider.java:220)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedPropertiesAndFields(DefaultModelDependencyProvider.java:181)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedDependencies(DefaultModelDependencyProvider.java:120)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.maybeFromCollectionElementType(DefaultModelDependencyProvider.java:220)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedPropertiesAndFields(DefaultModelDependencyProvider.java:181)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedDependencies(DefaultModelDependencyProvider.java:120)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.maybeFromRegularType(DefaultModelDependencyProvider.java:207)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedPropertiesAndFields(DefaultModelDependencyProvider.java:183)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.resolvedDependencies(DefaultModelDependencyProvider.java:120)
facturapp_1 | at springfox.documentation.schema.DefaultModelDependencyProvider.dependentModels(DefaultModelDependencyProvider.java:79)
facturapp_1 | at springfox.documentation.schema.CachingModelDependencyProvider$1.load(CachingModelDependencyProvider.java:50)
facturapp_1 | at springfox.documentation.schema.CachingModelDependencyProvider$1.load(CachingModelDependencyProvider.java:48)
facturapp_1 | at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3529)
facturapp_1 | at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2278)
facturapp_1 | at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2155)
facturapp_1 | at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2045)
facturapp_1 | at com.google.common.cache.LocalCache.get(LocalCache.java:3953)
facturapp_1 | at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3976)
My questions are:
why am I having such issue for just extending a simple DTO?
how can I solve this issue?
My mistake was using a domain class as a field instead of the DTO class for Address.
The question is still why there is a memory issue when launching the app and not when the method using the faulty class is called.
I have a Java webservice that use jax-rs.
I need to create a query through a petition. I use this to get full value from a table
#GET
#Override
#Produces({"application/xml", "application/json"})
public List<EstadoDia> findAll() {
return super.findAll();
}
Or use this to get the values by id.
#GET
#Path("{id}")
#Produces({"application/xml", "application/json"})
public EstadoDia find(#PathParam("id") PathSegment id) {
net.gleegoo.es.EstadoDiaPK key = getPrimaryKey(id);
return super.find(key);
}
My table example: EstadoDia
|ID | day |hour |send|
1 | 17 | 22:00|true
2 | 18 | 20:00|true
3 | 19 | 20:35|true
4 | 19 | 20:20|false
how to get all values of send ?