2. Create a New EntityΒΆ

In the previous step, we have an entity manager (entity_manager) and two entity classes (Player and Team). Now, we are going to create a new entity.

Let’s create one player.

sid = Player('Sid', 5)

session = entity_manager.open_session()

repository = session.repository(Player)
repository.persist(sid)
repository.commit()

From the code, sid = Player('Sid') is to create an object.

The rest are pretty simple.

  1. We open a DB (pseudo) session with open_session(),
  2. find the corresponding repository, referred as repository,
  3. tell the repository to push the new object on commit with persist(),
  4. and finally commit the change to the database.

Note

Alternatively, you may forget repository by using session directly by replacing everything about repository with the following code.

session.persist(sid)
session.flush()

where the session’s persist and commit are the same as the repository’s persist and flush respectively. For the sake of tutorial, we will keep using the repository approach.

Warning

The database session is pseudo for unsupported databases.

Note

The way we use open_session() here is to open an unsupervised session. The session connection cannot be closed by the supervising entity manager but the connection is still closed as soon as the process is terminated.

After this process, you can verify with MongoDB CLI (mongo sample_db) by running db.player.find(). You should be able to see the result on the screen like the following (except _id).

{
    "_id" : ObjectId("abc"),
    "name" : "Sid",
    "level": 5,
    "team" : null
}