In my application written in playframework 2.3.X I created an EntityManager (wanting to avoid to use persistence.xml) through the EntityManagerFactory interface. So all the reading database operations are carried out correctly, but when I call the persist method on the object in question the data is not saved.
Below the class to generate the EntityManager:
public final class PersistenceManager extends Controller {
@Transactional
@PersistenceContext
private static EntityManager getEM(CustomDB customDB) {
EntityManagerFactory entityManagerFactory = createEMFactory("jdbc:mysql://" + customDB.getDatabaseUrl() + ":" + customDB.getDatabasePort() + "/"+ customDB.getDatabaseName() + "?useSSL=true", customDB.getDatabaseUser(), customDB.getDatabasePassword());
EntityManager em = entityManagerFactory.createEntityManager();
return em;
}
public static EntityManagerFactory createEMFactory(String dbUrl, String dbUser, String dbPassword) {
Properties props = new Properties();
props.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
props.put("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
props.put("hibernate.archive.autodetection","class");
props.put("hibernate.show_sql","false");
props.put("hibernate.format_sql","true");
props.put("hbm2ddl.auto","create");
props.put("hibernate.connection.url", dbUrl);
props.put("hibernate.connection.username", dbUser);
if (StringUtils.hasText(dbPassword)) {
props.put("hibernate.connection.password", dbPassword);
}
PersistenceUnitInfo persistenceUnitInfo = new PersistenceUnitInfo() {
@Override
public Properties getProperties() {
return props;
}
@Override
public List<String> getManagedClassNames() {
return Arrays.asList(
Address.class.getName(),
City.class.getName(),
User.class.getName()
);
}
@Override
public String getPersistenceUnitName() {
return "Test";
}
@Override
public String getPersistenceProviderClassName() {
return HibernatePersistenceProvider.class.getName();
}
@Override
public PersistenceUnitTransactionType getTransactionType() {
return PersistenceUnitTransactionType.RESOURCE_LOCAL;
}
@Override
public DataSource getJtaDataSource() {
return null;
}
@Override
public DataSource getNonJtaDataSource() {
return null;
}
@Override
public List<String> getMappingFileNames() {
return Collections.emptyList();
}
@Override
public List<URL> getJarFileUrls() {
try {
return Collections.list(this.getClass()
.getClassLoader()
.getResources(""));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public URL getPersistenceUnitRootUrl() {
return null;
}
@Override
public boolean excludeUnlistedClasses() {
return false;
}
@Override
public SharedCacheMode getSharedCacheMode() {
return null;
}
@Override
public ValidationMode getValidationMode() {
return null;
}
@Override
public String getPersistenceXMLSchemaVersion() {
return null;
}
@Override
public ClassLoader getClassLoader() {
return null;
}
@Override
public void addTransformer(ClassTransformer transformer) {
}
@Override
public ClassLoader getNewTempClassLoader() {
return null;
}
};
HibernatePersistenceProvider hibernatePersistenceProvider = new HibernatePersistenceProvider();
return hibernatePersistenceProvider.createContainerEntityManagerFactory(persistenceUnitInfo, Collections.EMPTY_MAP);
}
}
This is my controller class to interact with the User entity:
public class UserController extends Controller {
@Transactional
public Result writeUser(CustomDB customDB ){
EntityManager em = PersistenceManager.getEM(customDB);
List<User> userList = em.createNamedQuery("userByName").setParameter("name","Jane").getResultList();
User user = userList.get(0);
user.phoneNumber("0123456789");
em.persist(user);
}
}
The query “userByName”, or in any case any type of read operation, takes the data, but the persist operation does not write into database. Where did I go wrong?