Question: EDIT: Added more code 

I'm listening for an event on my model like this: (this is my view)

```
var view = Marionette.ItemView.extend({

    tagName: 'tr',
    template: viewtemplate,

    modelEvents: {
        "change:highscore": "highchange"
    },

    highchange: function (model) {
        var elem = this.$('.send');
        console.log(elem);
        elem.hide();
        this.render();
    }
});

```

And my template:

```
<td class="cusername">{{username}}</td>
<td class="highscore">

<div class="box">
    <button class="send"></button>
        <span>Send</span>
    </button>
</div>

```

(this is inside my Marionette.ItemView.extend({...}); )  

When my model changes the highscore I want the send element to get hidden but when I do the above nothing changes. The console.log(elem) gives me the element as an object[div.send] which is the correct element, however it doesn't get hidden.

Does this have something to do with the lifecycle of marionette?
How can I change an element to be hidden as a result of an event on the model?

Answer: 
You can treat a ***Persistence Context*** as a container that has an ability to store ***entities*** that can be managed and synchronized with a database by using ***Entity Manager***. The following diagram depicts dependencies between JPA classes and interfaces:

![Relationships between JPA classes and interfaces](https://i.stack.imgur.com/WT3kQ.png)

---

A process of association an entity with a persistence context can be realized by executing:

* `EntityManager.persist` method on a newly created entity
* `EntityManager.merge` method on a detached entity
* `EntityManager.find` method with the given entity type and its primary key
* `EntityManager.createNativeQuery`, `EntityManager.createNamedQuery` and `EntityManager.createQuery` methods with SQL / JPQL / CriteriaAPI based queries

In case of transaction-scoped container-managed persistence context these methods need to be invoked within the scope of a transaction. 

```java
@Entity
public class Employee {
    @Id
    private int id; //determines persistent identity
    public Employee(int id) { this.id = id; }
}

```

Persistent identity allows to locate the entity across all entities of the same type by its primary key and to synchronize to the database in case of flushing or committing the persistence context (either manually by flush/commit or automatically).

```java
Employee emp = new Employee(1);

em.persist(emp);
boolean isManaged = em.contains(emp); //true, managed, id=1
boolean isFound = Objects.equals(emp, em.find(Employee.class, 1)); //true

```

Persisting the entity causes it to become managed (associated with persistence context). It dos not mean that the entity is synchronized to the database.

---

A process of disassociation an entity with a persistence context can be realized by executing:

* `EntityManager.detach` or `EntityManager.remove` methods on a managed entity; the difference between them is that a detached entity remain in the underlying database since removed one is not
* `EntityManager.commit`, `EntityManager.rollback`, `EntityManager.clear`, `EntityManager.close` methods which affects persistence context
* serialization as an effect of passing it to another tier or sending through a remote interface

```java
...
em.detach(emp);
isManaged = em.contains(emp); //false, detached, id=1
isFound = Objects.equals(emp, em.find(Employee.class, 1)); //false

```

Although detached entity still has assigned persistent identity it's no longer associated with the persistence context thus synchronized with the database. As a consequence any changes to a detached entity will not be persisted and committed unless it's merged into the persistence context. In such case it's possible that entity has been overridden by another party hence becoming stale, therefore id of entity being merged needs to be cleared/reassigned.
The persistence context is the code responsible for synchronizing the state of any attached entities with the state of the database. The EntityManager class is used to interact with the persistence context. Queries are executed via the EntityManager (which interacts with the persistence context) and keeps the entity state in sync with the database contents. When an entity is detached from the persistence context (for example by calling the detach method on the EntityManager) that entity's state is no longer kept in sync with the database.