reset password
Author Message
armandop
Posts: 32
Posted 08:45 May 23, 2014 |

Hello, 

Im running into an issue where i try to save data into the DB, everything looks like it works but when I attempt to verify that the data is present from pgAdmin i notice that none of the data is saving.  No errors in the logs.  

Even a simple example as:

 

        Player player = new Player();

        player.setEmail(email);

        player.setUsername(username);

        player.setPassword(password);

        entityManager.persist(player); 

 

Wont work.

 

cysun
Posts: 2935
Posted 08:49 May 23, 2014 |

Any write operation to the database needs to be in a transaction. You can manually create a transaction as in the Hibernate example, or use @Transactional as in the SpringMVC example.

armandop
Posts: 32
Posted 09:02 May 23, 2014 |

Thanks Prof. Here is the complete method. 

@Transactional

    public boolean createDBInsertTest(String username, String password, String email){   

 

        Player player = new Player();

        player.setEmail(email);

        player.setUsername(username);

        player.setPassword(password);

        entityManager.persist(player); 

    }

armandop
Posts: 32
Posted 09:07 May 23, 2014 |

Sorry hit submit too soon.  The above is the method im using to test this out. There has to be something else that im missing.   Ill take a look at the example once more.

cysun
Posts: 2935
Posted 09:11 May 23, 2014 |
armandop wrote:

Thanks Prof. Here is the complete method. 

@Transactional

    public boolean createDBInsertTest(String username, String password, String email){   

 

        Player player = new Player();

        player.setEmail(email);

        player.setUsername(username);

        player.setPassword(password);

        entityManager.persist(player); 

    }

Well, it can't be the complete method since it's declared as boolean but has no return value. Anyways, check your applicationContext.xml to make sure beans like dataSource, transactionManager etc. are in place; check if your DaoImpl is annotated as @Repository; check if you have a <component-scan> that scans your DaoImpl; check if you have more than one <component-scan> that scan some beans twice (e.g. one <component-scan base-package="ttt" /> and another <component-scan base-package="ttt.model" />) - this could cause problems like the one you experienced.

xytian
Posts: 70
Posted 09:21 May 23, 2014 |

.

Last edited by xytian at 09:26 May 23, 2014.
armandop
Posts: 32
Posted 09:40 May 23, 2014 |

It all looks ok.   tried adding in a flush to force the commit but now getting javax.persistence.TransactionRequiredException: no transaction is in progress

 

Ill keep digging.

xytian
Posts: 70
Posted 10:43 May 23, 2014 |

From what I noticed, as the professor said, you put all controllers under ttt and have <component-scan base-package="ttt" /> which could cause the problem. You didn't have transactionManager in applicationContext.xml.

entityManager should be annotated with @PersistenceContext, player in the controller should have an annotation @Autowired.

You handcrafted the script which may not work with hibernate. Simply change "envite" to "ttt" and Hbm2ddl.java should work. Using the generated schema also enables you to check whether the annotations in the model class is correct.