0

I'm currently working on a project and I want to see what damage it can do if I don't embrace my code with try-catch block when persisting object into database. Here is my code down below that i use as test.

public class NewEventServiceBean implements NewEventService {

    @PersistenceContext(name = "example")
    EntityManager manager;

    @Resource
    private UserTransaction userTransaction;

    @Override
    public void createNewEvent(String title, String content) throws Exception
            {
            userTransaction.begin();

            Event event = new Event();
            Content cont = new Content();

            cont.setContent(content);
            event.setTitle(title);
            event.setCont(cont);

            manager.persist(event);
            manager.persist(cont);

            userTransaction.commit();

    }

In the database i have this Event table that has a foreign key to Content table.

And my question is if It's possible that Event object is persisted in to the database even if I cause something wrong when persisting the content class. Or what is the disadvantages of not embracing the code with try catch and rollback?

I've tried to cause an error when persisting the content object, but the Event is not persisted into the datbase even if everything is correct in that class.

Krm1337
  • 1
  • 3
  • Would you please also add the source code of the entities `Event` and `Content`? – ltlBeBoy Dec 07 '18 at 08:19
  • It's just a basic class where you can figure out what kind of variables (just by watching the set-methods) it has and what kind of methods. – Krm1337 Dec 11 '18 at 09:19
  • Yes, I know what these classes are for. But the annotations within these classes are probably wrong. And if we know how the classes look like, then we can help you much better. If you carefully read [this](https://stackoverflow.com/q/13027214/5223047) and [this](https://stackoverflow.com/q/28720498/5223047) question and the answers, then you might find the solution by yourself. Furthermore you can remove `manager.persist(event);` from your code then. – ltlBeBoy Dec 12 '18 at 08:19

0 Answers0