Tag is an entity and I remove tags with this method:
public static <T> boolean deleteById(Class<? extends BaseEntity> clazz, Long id) {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
session.beginTransaction();
T e = get(clazz, id);
if (e != null) {
session.delete(e);
session.getTransaction().commit();
return true;
} else {
return false;
}
} finally {
session.close();
}
}
Next thing, I read the list with Tags again with this method:
public static List<Tag> listTags() {
Session session = HibernateUtil.getSessionFactory().openSession();
Query q = session.createQuery("FROM Tag tag");
List<Tag> tags = (List<Tag>) q.list();
session.close();
return tags;
}
The problem is that when deleting and reselecting all Tags the removed Tag is in the list although not in the database. when I run listTags() a second time, by clicking a link the object is removed and I get the correct list.
Does anyone know why?
Try to add flush() and use commit() like below:
...
session.delete(e);
session.flush();
session.beginTransaction().commit();
...
For more info go to session.getTransaction() vs session.beginTransaction()
I had a similar problem with Hibernate while migration from MySQL to MariaDB, were multiple transactions were being open and not closed...
the solution was removing the opening of the transactions as they were not required anyway....
Related
I have a DAO User with Hibernate framework.
I am doing some Junit test about it. Testing create/update/delete on user, works for create but not for update and delete but my test passed.
I need to verify what is wrong with my update, delete functions.
I tried to add a session.save() befose the update NOK
I tried to replace session.update() to session.saveOrUpdate() NOK
I tried to use the user parameter instead of create a new user NOK
Junit function update/delete only displaying update and delete
#Test
public void testCreateUpdateDeleteUser(){
User userCRUD = new User("morganeflamant#gmail.com", "admin", new
Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
userCRUD = serviceFacade.getUserDao().findUserById(userCRUD.getId_user());
Assert.assertNotNull(userCRUD);
userCRUD.setId_user(userCRUD.getId_user());
userCRUD.setEmail("testmorgane#gmail.com");
userCRUD.setRole("member");
userCRUD = serviceFacade.getUserDao().updateUser(userCRUD);
Assert.assertNotNull(userCRUD);
Assert.assertNotNull(userCRUD.getUpdateAt());
userCRUD = serviceFacade.getUserDao().findUserById(userCRUD.getId_user());
Assert.assertTrue(serviceFacade.getUserDao().deleteUser(userCRUD));
}
Userdao update and delete function:
#Override
public User updateUser(User user) {
Session session = SingletonHibernate.getSessionFactory().openSession();
session.beginTransaction();
User u = new User(user.getId_user(), user.getEmail(), user.getRole(), user.getCreateAt(), user.getUpdateAt());
u.setId_user(user.getId_user());
u.setEmail(user.getEmail());
u.setRole(user.getRole());
u.setCreateAt(user.getCreateAt());
u.setUpdateAt(user.getUpdateAt());
session.saveOrUpdate(u);
session.close();
return u;
}
#Override
public boolean deleteUser(User user) {
Session session = SingletonHibernate.getSessionFactory().openSession();
session.beginTransaction();
User u = (User) session.get(User.class, user.getId_user());
if(u != null){
session.delete(u);
return true;
}
return false;
}
In my db I actually have :
60
morganeflamant#gmail.com
admin
2019-07-19 13:38:19
2019-07-19 13:38:19
but I am supposed to have this:
60
testmorgane#gmail.com
admin
2019-07-19 13:38:19
2019-07-19 13:38:19
for the update part and the delete part I am not supposed to have the user 60.
UPDATE:
adding this line :
session.getTransaction().commit();
after the session.save() and the changes are done.
Thank you for your comments !
i've come across a problem in these days, which would be simple for other languages, like php, but the project I'm doing is in Spring MVC.
The question is: In Spring MVC, how can i delete an entity with two attributes ids coming from this entity?
Example: "Delete from Entity Where id1 =: id1 and id2 =: id2" (This is the query that i want)
Thanks for the attention.
What i was trying ...
public boolean remover(int idUsuario, int idCategoria) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager manager = factory.createEntityManager();
String hqlStr = "delete from UsuarioEscolheCategoria where idUsuario = :idUsuario and idCategoria = :idCategoria";
Query query = null;
try {
query = manager.createQuery(hqlStr);
query.setParameter("idUsuario", idUsuario);
query.setParameter("idCategoria", idCategoria);
query.executeUpdate();
manager.close();
factory.close();
return true;
}catch(Exception e) {
return false;
}
}
If i take the exception, it gives me:
String hqlStr = "delete from UsuarioEscolheCategoria where usuario.idUsuario = :idUsuario and categoria.idCategoria = :idCategoria";
The important part is usuario.idUsuario and categoria.idCategoria. That way you're making a reference to the attribute type Usuario, which is on your model class.
you prblem is the session factory, check how you have created it, here a simple usefull example:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
#Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public void deleteById(Integer id) {
Query query = getSession().createSQLQuery("delete from TABLE where id = :t_id");
query.setInteger("t_id", id);
query.executeUpdate();
}
I'm curious what should be result when I create object, Then I delete object and then I try to load this object from database. I guess it should be "null". In one table I receive "null" in second table i receive nothing.
Create:
public void createPerson(Person p) {
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
logger.info("Person saved successfully!");
}
Delete:
public void deletePerson(int id) {
session.beginTransaction();
Person p = getPersonById(id);
if(p != null){
session.delete(p);
session.getTransaction().commit();
}
if(getPersonById(id) == null){
logger.info("Person deleted successfully!");
}else{
logger.info("Something went wrong! Person hasn't been deleted!");
}
}
and load:
public Person getPersonById(int id) {
session.beginTransaction();
Person p = (Person) session.load(Person.class, id);
logger.info("Person loaded successfully!");
return p;
}
And I receive nothing. What is the problem?
As per my understanding you should flush your context after create/delete operation.The flush process synchronizes database state with session state by detecting state changes and executing SQL statements.
if(p != null){
session.delete(p);
session.getTransaction().commit();
session.setFlushMode(FlushModeType.COMMIT);
}
You can read about flush mode Hibernate flush mode.
Where is the commit in deletePerson? and the commit again in getPersonById?
Fix the code you would see that you don't get the entity back by ID one you delete it.
In Hibernate after session.save(obj) without committing the transaction. If I close the session, when I try to fetch, I will get the data which is in session, i.e the object I didn't commit including that
public static void insert() {
Session session = Hibernateutil.getSessionFactory().openSession;
Leave leave = new Leave();
leave.setUser("nat");
session.save(leave);
session.close();
}
public static List fetch() {
Session session = Hibernateutil.getSessionFactory().openSession;
List li = session.createQuery("from Leave").list();
}
Now I am using jpa with hibernate , when i was done getEntityManager.persist(objects) then i will ask for user confirmation like continue and rollback using user interface
private List<TempCustomers> tempCustomer =new ArrayList<TempCustomers>();
#Begin(join = true)
public String migrateData() {
log.info("Mobee Migrate Customer Size :"+doTempCustomers.size());
for(DoTempCustomers tempCustomers:doTempCustomers){
try {
TempCustomers temp=new TempCustomers();
BeanUtils.copyProperties(temp, tempCustomers);
tempCustomer.add(temp);
getEntityManager().persist(temp);
}catch (Exception e) {
// TODO: handle exception
return "null";
}
}
log.info("Size........."+tempCustomer.size());
return "null";
}
#Begin(join = true)
public String updatedData(){
log.info("Size of Customers :"+tempCustomer.size());
log.info("Decision ..."+decision);
try{
if(decision.equals("Continue")){
for(TempCustomers tempCust:tempCustomer){
TempCustomers temp=new TempCustomers();
BeanUtils.copyProperties(temp, tempCust);
log.info("updated Sucessfully");
getEntityManager().getTransaction().commit();
}}else{
getEntityManager().getTransaction().rollback();
}
}
catch(Exception e){
}
}
please help me how to do continue and rollback in jpa with hibernate when getEntityManager().persist() is done.
To commit with JPA:
entityManager.getTransaction().commit();
To rollback with JPA:
entityManager.getTransaction().rollback();
Call either of these methods after your call to persist to perform the desired action. In your case entityManager would be replaced with the call to retrieve the entityManager, getEntityManager()
Reference: http://www.objectdb.com/java/jpa/persistence/store