I have a jakarta EE based application where I wanted to test out jpa with simple implementation. I encountered a strange behaviour. My rest endpoints like /jpa/rest/company/{companyId} returns data i had fixtured
but when I check these data via database client like Dbeaver I can’t see data in tables. It’s empty:
Configuration of payara server with of JDBC Connection Pools and JDBC Resources with mysql-connector-java-8.0.22 driver are:
Company Resource with Get endpoint:
@Path("/company")
@RequestScoped
public class CompanyResource {
@Inject
private CompanyService companyService;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
public Company getCompany(@PathParam("id") Long id) {
return companyService.findCompany(id);
}
}
Company Service injected into CompanyResource:
@ApplicationScoped
@Transactional
public class CompanyService {
@PersistenceContext
private EntityManager entityManager;
public Company findCompany(Long id) {
return entityManager.find(Company.class, id);
}
@Transactional
public void insertTestCompanies() {
Company company1 = new Company();
company1.setId(4L);
company1.setName("Payara");
entityManager.persist(company1);
Company company2 = new Company();
company2.setId(5L);
company2.setName("ACME");
entityManager.persist(company2);
Company company3 = new Company();
company3.setId(6L);
company3.setName("TEST");
entityManager.persist(company3);
entityManager.flush();
}
}
Persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence
https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="TestUnit">
<jta-data-source>jdbc/local-mysql</jta-data-source>
<properties>
<property name="jakarta.persistence.schema-generation.database.action" value="create"/>
<property name="jakarta.persistence.sql-load-script-source" value="META-INF/defaultdata.sql"/>
<property name="eclipseLink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
defaultdata.sql initliaization script:
INSERT INTO company(`id`,`name`) VALUES(1,'Payara');
INSERT INTO company(`id`,`name`) VALUES(2,'ACME');
INSERT INTO company(`id`,`name`) VALUES(3,'TEST');
Has someone an idea where these data are saved instead of my local mysql database? Is this a problem of uncommited transaction which are available for rest endpoints? I would be greate for help with understanding what’s going on here