Unit Test failire: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread
See original GitHub issueOverview of the issue
I am using JHipster Generator v4.0.7 and I create an entity like that:
entity Country {
id Long,
name String required
}
entity Zone {
id Long,
name String required
}
relationship OneToMany {
Zone to Country{zone(name) required},
}
When I generate the code, JHipster generator generate failed unit test. I have this stacktrace:
-------------------------------------------------------------------------------
Test set: xxx.yyy.zzz.CountryResourceIntTest
-------------------------------------------------------------------------------
Tests run: 11, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.134 sec <<< FAILURE! - in xxx.yyy.zzz.web.rest.CountryResourceIntTest
equalsVerifier(xxx.yyy.zzz.web.rest.CountryResourceIntTest) Time elapsed: 0.054 sec <<< ERROR!
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:282)
at com.sun.proxy.$Proxy165.persist(Unknown Source)
at fr.accs.advalorem.web.rest.CountryResourceIntTest.createEntity(CountryResourceIntTest.java:101)
at xxx.yyy.zzz.web.rest.CountryResourceIntTest.initTest(CountryResourceIntTest.java:115)
It seems that equalsVerifier method should be @Transactional because it call the @Before method
Suggest a Fix
Replace
@Test
public void equalsVerifier() throws Exception {
TestUtil.equalsVerifier(Country.class);
}
By
@Test
@Transactional
public void equalsVerifier() throws Exception {
TestUtil.equalsVerifier(Country.class);
}
JHipster Version(s)
JHipster generator v4.0.7
Entity configuration(s) entityName.json
files generated in the .jhipster
directory
Country.json:
{
"fluentMethods": true,
"relationships": [
{
"relationshipType": "many-to-one",
"relationshipValidateRules": "required",
"relationshipName": "zone",
"otherEntityName": "zone",
"otherEntityField": "name"
}
],
"fields": [
{
"fieldName": "name",
"fieldType": "String",
"fieldValidateRules": [
"required"
]
}
],
"changelogDate": "20170307151049",
"entityTableName": "country",
"dto": "no",
"pagination": "pager",
"service": "serviceImpl"
}
Zone.json
{
"fluentMethods": true,
"relationships": [
{
"relationshipType": "one-to-many",
"relationshipName": "country",
"otherEntityName": "country",
"otherEntityRelationshipName": "zone"
}
],
"fields": [
{
"fieldName": "name",
"fieldType": "String",
"fieldValidateRules": [
"required"
]
}
],
"changelogDate": "20170307151049",
"entityTableName": "zone",
"dto": "no",
"pagination": "pager",
"service": "serviceImpl"
}
- [ X] Checking this box is mandatory (this is just to show you read everything)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Spring - No EntityManager with actual transaction available for ...
The operation was attempted from a JUnit test class. ... GOT javax.persistence. ... with actual transaction available for current thread.
Read more >Error No EntityManager with actual transaction available for ...
I get this error when trying to invoke "persist" method to save entity model to database in my Spring MVC web application.
Read more >WB Blog - Spring @Transactional Annotation - Wilfried Barth
No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.
Read more >Getting 'No EntityManager with actual transaction available for ...
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' ...
Read more >Hapi 3.4.0 Spring Boot Issue:No EntityManager with actual ...
Caused by: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I understand why our Travis build didn’t detect that:
"relationshipValidateRules": "required"
in our entities intravis/samples/.jhipster/
Thanks @pascalgrimaud !!! This was worrying me, I’m happy to have the answer.