JPA - database gets updated only when method is #Transactional - java

I've encountered a problem I don't really understand - unless a method working with the DAO is annotated as #Transactional, the underlying database doesn't get updated. My app runs on JPA/Hibernate, Spring and Wicket. Why is that?
DAO:
#Repository(value = "userDao")
public class UserDaoJpa implements UserDao {
#PersistenceContext
private EntityManager em;
public User findById(Long id) {
return em.find(User.class, id);
}
public List findAll() {
Query query = em.createQuery("select e from User e");
return query.getResultList();
}
public User create(User user) {
em.persist(user);
return user;
}
public User update(User user) {
return em.merge(user);
}
public void delete(User user) {
user = em.merge(user);
em.remove(user);
}
}
Service:
#Service(value = "userManager")
public class UserManagerImpl implements UserManager {
#Autowired
UserDao dao;
public void setUserDao(UserDao dao) {
this.dao = dao;
}
public List getUsers() {
return dao.findAll();
}
public User getUser(String userId) {
return dao.findById(Long.valueOf(userId));
}
public void saveUser(User user) {
dao.update(user);
}
#Transactional
public void removeUser(User user) {
dao.delete(user);
}
}
In case I leave out the #Transactional annotation, the database doesn't get updated.

Well thats normal:
Every database manipulation in the CRUD Scheme needs it's transaction boundaries. Without those boundaries, nothing gets actually written in the DB.
A Transaction is a collection of DB Manipulations (inserts, Updates) whoich have all to be successfull or the whole action gets undone by the DB. That's why you have to tell Hibernate when a Transaction starts and ends, so hibernate can tell which actions have to be regardes as Units of Work. Without transaction boundaries the final commit to the database never happens.
hope that helped

Try to flush and commit.

Related

How to create a MongoDB DAO layer like JPA using the entity manager does to facilitate all other DAO class in term of design?

Knowing that there are some frameworks to do it in real-world situation. As I learning from scratch, I wonder is that possible to create pure MongoDB DAO as a layer on top of the Service class to do CRUD operation that also facilitate other DAO use it?
For example, below is my Generic DAO class to operate CRUD process.
public interface IGenericDAO<T> {
public T create(T t);
public T update(T t);
public T get(Object id);
public void delete(Object id);
public List<T> listAll();
public Long count();
}
Then, DAO class should implements its operations
public class UserDAO implements IGenericDAO<User> {
MongoDatabase database = dBUtils.getMongoDB();
MongoCollection <Document> userTbl = database.getCollection("User");
public UserDAO() {
super();
}
#Override
public User create(User user) {
return user;
}
// other CURD below
}
User Service class
public class UserService {
private UserDAO userDAO;
public UserService() {
}
public void listUser() {
// need to test
List<User> listUsers = userDAO.listAll();
}
public void create(User user) {
// this what I want to see. user is saved to db here
try {
MongoDatabase database = dBUtils.getMongoDB();
assert mdb != null;
// create or get collection
MongoCollection<User> userTbl = database.getCollection("Users", User.class);
User userDoc = new User();
userTbl.insertOne(userDoc);
} catch (Exception e) {
e.printStackTrace();
}
}
Having said that, I want to put MongoDB "layer" between the DAO and Service class to do the CRUD operation. I wonder is it necessary, otherwise, how to do it to help the UserService with UserDAO class?

How to update object parameter in DAO?

I'm new at Java and trying to understand how MVC architecture goes. Forgive me if I'm wasting your time. I wrote a DAO Service, it handles the crud model (get, read, update, delete).
public List<User> getUsers();
public User getUser(Long userId);
public void createUser(User user);
public void updateUser(User user);
public void delete(Long userId);
}
here are my abstract DAO functions.
#Override
#Transactional
public void updateUser(User user) {
em.merge(user);
}
and the controller:
#PutMapping(value = "/{userId}", produces = "application/json")
public ResponseEntity<UserDTO> update(#PathVariable Long userId, #RequestBody UserDTO user){
try{
service.updateUser(user);
return new ResponseEntity<>(HttpStatus.OK);
} catch (HttpClientErrorException p){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} catch (HttpServerErrorException.InternalServerError u){
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
in the service:
#Override
public void updateUser(UserDTO user) {
userDAO.updateUser(ApiDTOBuilder.userDTOToUser(user));
}
How can I pass the userId and set the new parameters into the current user object?
First you need to fetch the User using DAO method. And then you need to set the values to the User entity. As you are using #Transactional, spring will take care of updating the values.
#Override
#Transactional
public void updateUser(Long userId,UserDTO userdto) {
User user= getUser(userId);
user.setFirstName(userdto.getFirstName());
user.setLastName(userdto.getLastName())
}
Also if you are not using Spring Boot, then you need to enable transaction management by using
#EnableTransactionManagement

How to persist/commit entity to database immediately in Activiti Service Task

I have a need to persist(insert) a entity to database immediately when the save (or saveAndFlush) code is called.
However although the entity is created in the context it is not persisted in the database immediately.
We are using Spring Boot.
public interface MessageRepository extends JpaRepository<MessageEntity, Long> {
}
In the Service class
#Service
public class TestService {
#Autowired
private MessageRepository messageRepository;
#Transactional
public MessageEntity saveMessage(MessageEntity entity) throws Exception {
entity = messageRepository.saveAndFlush(entity);
return entity;
}
}
Though the entity is created it is not persisted/committed to the database immediately.
We are facing this issue within the Activiti Tasks only.
Any feedback will be appreciated.
This worked.
#Component
public class MessageRepositoryCustomImpl implements MessageRepositoryCustom {
#PersistenceContext
EntityManager entityManager;
#Override
#Transactional(propagation = Propagation.REQUIRES_NEW)
public MessageEntity saveImmediate(MessageEntity entity) {
entityManager.persist(entity);
return entity;
}
}
One way of overcoming this situation is by taking advantage of the REQUIRES_NEW transaction attribute.
In your situation you would have to create a new repository:
public interface MessageRepositoryCustom{
void saveAndFLush(MessageEntity ent);
}
public MessageRepositoryCustomImpl implements MessageRepositoryCustom{
#Autowired
private SessionFactory sessionFactory;
#Transactional(propagation = Propagation.REQUIRES_NEW)
void saveAndFLush(MessageEntity ent){
Session session = sessionFactory.getCurrentSession();
session.persist(ent);
}
}
Then in your service you would use that repository:
#Transactional
public MessageEntity saveMessage(MessageEntity entity) throws Exception {
entity = messageRepositoryCutom.saveAndFlush(entity);
// other processing
return entity;
}
}
Now after the messageRepositoryCutom.saveAndFlush method has finished processing the entity will be physically persisted in the database as it was created in a separate transaction which has been commited.

Spring Boot and multiple databases

I've setup a basic spring project with a single database connection.
In the application.properties file I have the database settings:
spring.datasource.url = jdbc:mysql://192.168.1.19/ticket
spring.datasource.username = dbusername
spring.datasource.password = dbpassword
I've created a base DAO class which other DAOs extend:
#Transactional
public class Dao<E> {
#PersistenceContext
private EntityManager entityManager;
private Class<E> entityClass;
public Dao(Class<E> entityClass) {
this.entityClass = entityClass;
}
public void create(E object) {
entityManager.persist(object);
return;
}
public void delete(E object) {
if (entityManager.contains(object)) {
entityManager.remove(object);
} else {
entityManager.remove(entityManager.merge(object));
}
return;
}
#SuppressWarnings("unchecked")
public List<E> getAll() {
return entityManager.createQuery("from " + entityClass.getName()).getResultList();
}
public E get(long id) {
return entityManager.find(entityClass, id);
}
public void update(E object) {
entityManager.merge(object);
return;
}
}
Here's a sample entity that extends the base DAO:
#Repository
public class PersonDao extends Dao<Person> {
public PersonDao() {
super(Person.class);
}
}
Currently this uses a single database, but I need to be able to add a second database, and somehow define in each DAO which datasource to use. Each DAO will only use a single database, so there's no requirement for a DAO to be able to connect to multiple databases.
I've done some research, and that seems to suggest I need to use JdbcTemplate? but I can't seem to find a tutorial that matches my need. Also, at the minute the entityManager is injected into the DAO, but the JdbcTemplate examples I've looked at don't seem to use the entityManager, which is slightly confusing.
database.password1=<password1>
database.url1=jdbc\:mysql\://localhost\:3306/twodbone
database.username1=<username1>
database.password2=<password1>
database.url2=jdbc\:mysql\://localhost\:3306/twodbtwo
database.username2=<username2>
database.driverClassName=com.mysql.jdbc.Driver
In this way you can add the multiple databases and configure both hibernate.cfg.xml file and applicationContext.xml file also..
#Repository
public class FooRepository
{
#PersistenceContext
private EntityManager entityManager;
#Autowired(required = true)
private JdbcTemplate jdbcTemplate;
public void saveFoo(Foo foo)
{
this.entityManager.persist(foo);
}
public List<SomeReportPojo> getSomeReport()
{
return this.entityManager.queryForList("SELECT .. ",SomeProjectPojo.class);
}
}
this.jdbcTemplate should be kept rather than this.entityManager for jdbc templetes
this is simple example

Insert Data only once after Hibernate started

I'm using hibernate 4.1.2
And I want insert some data into the table.
I know that it can be realized with sql in hibernate configuration in this way:
<property name="hibernate.hbm2ddl.import_files" value="/file1.sql,/file2.sql"/>.
But, are there any other ways insert automatically data only once in Java code after hibernate started?
I want do it like this:
public Role getRoleByName(EnumRole name)
{
return (Role) sessionFactory.getCurrentSession()
.createQuery("from Role where name = :name")
.setParameter("name", name).uniqueResult();
}
public void insertRoles(){
for(EnumRole role:EnumRole.values())
{
Role r=getRoleByName(role);
if(r==null)
{
r=new Role();
r.setName(role);
r.setDescription(role.getDescription());
sessionFactory.getCurrentSession().save(r);
}
}
EnumRole:
public enum EnumRole {
ROLE_CLIENT("РОЛЬ КЛИЕНТА"),
ROLE_ADMIN("РОЛЬ АДМИНСТРАТОРА"),
ROLE_CONSUMER("РОЛЬ КОМПАНЬОНА"),
ROLE_ANONYMOUS("НЕ АВТОРИЗОВАННЫЙ ПОЛЗОВАТЕЛЬ");
EnumRole(String descriptin)
{
this.descriptin=descriptin;
}
public String getDescription()
{
return this.descriptin;
}
private String descriptin;
}
You need to create any Spring bean with #PostConstruct annotated method and for example create transaction using PlatformTransactionManager:
#Service
public class AnyBean {
#Autowired
private PlatformTransactionManager transactionManager;
#Autowired
private SessionFactory sessionFactory;
#PostConstruct
private void init() {
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallback<Object>() {
#Override
public Object doInTransaction(TransactionStatus transactionStatus) {
// HERE YOU CODE
for(EnumRole role:EnumRole.values())
{
Role r = getRoleByName(role);
if(r==null)
{
r=new Role();
r.setName(role);
r.setDescription(role.getDescription());
sessionFactory.getCurrentSession().save(r);
}
}
return null;
}
});
}
}
I hope this helped.
Create a service with method insertRoles() marked with #Transactional and call service.insertRoles().
Or, more easy:
create a transaction
add roles
commit (or manage rollback if error occured)
Do this stuff after your application startup has been completed

Categories

Resources